为什么运行 Flask 开发服务器会自己运行两次? [英] Why does running the Flask dev server run itself twice?
问题描述
我正在使用 Flask 开发网站,在开发过程中我使用以下文件运行烧瓶:
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 (the rest works as expected), I simply wonder why it behaves like this? Any ideas?
推荐答案
Werkzeug 重新加载器产生一个子进程,以便它可以在每次代码更改时重新启动该进程.Werkzeug 是在您调用 app.run()
时为 Flask 提供开发服务器的库.
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()
.
参见_reloader 功能代码;您的脚本使用 subprocess.call()
再次 运行.
See the restart_with_reloader()
function code; your script is run again with subprocess.call()
.
如果您将 use_reloader
设置为 False
,您将看到行为消失,但同时您也失去了重新加载功能:
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)
您也可以在使用 flask run
命令时禁用重载器:
You can disable the reloader when using the flask run
command too:
FLASK_DEBUG=1 flask run --no-reload
您可以使用 werkzeug.serving.is_running_from_reloader
函数 如果你想检测你何时处于重载子进程:
You can use the werkzeug.serving.is_running_from_reloader
function if you wanted to detect when you are in the reloading child process:
from werkzeug.serving import is_running_from_reloader
if is_running_from_reloader():
print(f"################### Restarting @ {datetime.utcnow()} ###################")
但是,如果您需要设置模块全局变量,那么您应该使用 @app.before_first_request
装饰器 并让该函数设置这样的全局变量.当第一个请求进来时,它会在每次重新加载后只调用一次:
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(f"########### Restarted, first request @ {datetime.utcnow()} ############")
请注意,如果您在使用分叉或新子进程处理请求的全尺寸 WSGI 服务器中运行它,before_first_request
处理程序可能被调用每个新的子流程.
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 开发服务器会自己运行两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!