Why does running the Flask dev server run itself twice?
来源:互联网 发布:徐家福 知乎 编辑:程序博客网 时间:2024/06/03 15:55
I’m using Flask for developing a website and while in development I run flask using the following file:
#!/usr/bin/env pythonfrom datetime import datetimefrom app import appimport configif __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 ###################
Answer:
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 osif 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_requestdef 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.
- Why does running the Flask dev server run itself twice?
- FAQ: Why Does The Grader Not Run My Program Correctly? 【USACO】
- Delete/Run scripts itself
- hint:this error often happens when you are running the packager(local dev server)
- Run on Server报错:The server does not support version 3.0 of the J2EE Web module specification
- run on server的时候,报出The server does not support version 3.0 of the J2EE Web module specification
- eclipse 中项目->run as->run on server无法在tomcat6下运行,提示The server does not support version 3.0 of the J2E
- How Does the Garbage Collector Run?
- The run destination''''is not valid for Running the scheme
- Why does SystemParametersInfo hang when I pass the SPIF_SENDCHANGE flag?
- Why does TypeScript have to be the answer to anything?
- Why Does GoldenGate Report The Lag Is Unknown
- Why does the PDB format change every release?
- QuickFAST:Why can't I run the performance test? Why can't I run the example programs?
- Virtualbox The character device /dev/vboxdrv does not exist.
- the methods running X server on CoLinux
- tomcat run show the error: Resource '/Servers2' does not exist
- The reason of /var/run/wpa_supplican directory does not exist
- DSS 代码分析【Reliable UDP之超时时间计算】
- Nginx 反向代理学习及实例笔记
- Centos系统基于Docker安装tensorflow
- 用java程序实现mysql的分页效果
- 第一部分:基础问题 1.3
- Why does running the Flask dev server run itself twice?
- C++ explicit的作用
- Java并发编程:volatile关键字解析
- 微信公众号菜单编辑
- 如何扩大CMD窗口显示的行数
- Android–taskAffinity属性
- 第九周项目1---二叉树算法库
- SparkSQL的发展历史
- 147. Insertion Sort List