为什么运行Flask dev服务器运行两次? [英] Why does running the Flask dev server run itself twice?

查看:395
本文介绍了为什么运行Flask dev服务器运行两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Flask 开发一个网站,而在开发过程中,我使用以下文件运行烧瓶:

 #!/ usr / bin / env python $ b $ from datetime import datetime $ b $ from app import app 
import config

if __name__ =='__main__':
print'################### Restarting @',datetime.utcnow() ,'###################'
app.run(port = 4004,debug = config.DEBUG,host ='0.0.0.0')

当我启动服务器,或者因为文件已经更新而自动重新启动时,它总是显示打印

  ################### Restarting @ 2014- 08-26 10:51:49.167062 ################### 
###################重新启动@ 2014-08-26 10:51:49.607096 ###################

虽然这不是一个真正的问题(剩下的一切按预期工作),但我只是想知道为什么它的行为如此呢?任何想法?

解决方案

Werkzeug重载程序产生一个子进程,以便每次代码更改时重新启动该进程。 Werkzeug是当您调用 app.run()时为开发服务器提供Flask的库。



restart_with_reloader()功能码;用 subprocess.call()再次运行脚本 use_reloader False 你会看到行为消失,但是你也失去了重载功能:

  app.run(port = 4004,debug = config.DEBUG,host ='0.0.0.0',use_reloader = False)

使用 flask run 命令时可以禁用重载器

  FLASK_DEBUG = 1 flask run --no-reload 

如果您想要检测何时处于重新加载子进程中,可以查找 WERKZEUG_RUN_MAIN 环境变量:

  import os 
if os.environ.get('WERKZEUG_RUN_MAIN')=='true':
print'################### Restarting @ {} ###################'。format(
datetime.utcnow())

但是,如果您需要设置模块全局变量,那么哟你应该改用 @ app.before_first_request 装饰器上的一个函数,并有该函数设置这样的全局变量。在第一次请求进入时,它会在每次重新加载时调用一次:

  @ app.before_first_request 
def before_first_request():
print'###########重新启动,首先请求@ {} ############'。格式(
datetime。 utcnow())

请注意,如果您在全尺寸WSGI服务器使用分叉或新的子进程来处理请求,可以为每个新的子进程调用 before_first_request 处理程序。


I'm using Flask for developing a website and while in development I run flask using the following file:

#!/usr/bin/env python
from datetime import datetime
from app import app
import config

if __name__ == '__main__':
    print '################### Restarting @', datetime.utcnow(), '###################'
    app.run(port=4004, debug=config.DEBUG, host='0.0.0.0')

When I start the server, or when it auto-restarts because files have been updated, it always shows the print line twice:

################### Restarting @ 2014-08-26 10:51:49.167062 ###################
################### Restarting @ 2014-08-26 10:51:49.607096 ###################

Although it is not really a problem (everything for the rest works as expected), I simply wonder why it behaves like this? Any ideas?

解决方案

The Werkzeug reloader spawns a child process so that it can restart that process each time your code changes. Werkzeug is the library that supplies Flask with the development server when you call app.run().

See the restart_with_reloader() function code; your script is run again with subprocess.call().

If you set use_reloader to False you'll see the behaviour go away, but then you also lose the reloading functionality:

app.run(port=4004, debug=config.DEBUG, host='0.0.0.0', use_reloader=False)

You can disable the reloader when using the flask run command too:

FLASK_DEBUG=1 flask run --no-reload

You can look for the WERKZEUG_RUN_MAIN environment variable if you wanted to detect when you are in the reloading child process:

import os
if os.environ.get('WERKZEUG_RUN_MAIN') == 'true':
    print '################### Restarting @ {} ###################'.format(
        datetime.utcnow())

However, if you need to set up module globals, then you should instead use the @app.before_first_request decorator on a function and have that function set up such globals. It'll be called just once after every reload when the first request comes in:

@app.before_first_request
def before_first_request():
    print '########### Restarted, first request @ {} ############'.format(
        datetime.utcnow())

Do take into account that if you run this in a full-scale WSGI server that uses forking or new subprocesses to handle requests, that before_first_request handlers may be invoked for each new subprocess.

这篇关于为什么运行Flask dev服务器运行两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆