如何停止Flask在调试模式下初始化两次? [英] How to stop Flask from initialising twice in Debug Mode?
问题描述
在Python中构建Flask服务并设置调试模式时,Flask服务将初始化两次。当初始化加载缓存等时,这可能需要一段时间。在开发(调试)模式下,必须这样做两次会很烦人。在调试模式下,Flask服务只会初始化一次。
h2_lin>解决方案最简单的做法是将 use_reloader = False
添加到应用的调用中.run
- 即: app.run(debug = True,use_reloader = False)
或者,您可以检查 <$ c 但是,如果您希望在加载过程中除 以外的行为发生,那么情况会更复杂一些: When building a Flask service in Python and setting the debug mode on, the Flask service will initialise twice. When the initialisation loads caches and the like, this can take a while. Having to do this twice is annoying when in development (debug) mode. When debug is off, the Flask service only initialises once. How to stop Flask from initialising twice in Debug Mode? The simplest thing to do here would be to add Alternatively, you can check for the value of However, the condition is a bit more convoluted when you want the behavior to happen any time except in the loading process:
这篇关于如何停止Flask在调试模式下初始化两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ p $ if os.environ.get($ c $> $ c $> $> $ c $> WERKZEUG_RUN_MAIN)==true:
#reloader已经运行 - 在这里做你想做的事情
如果不是app.debug或os.environ.get( WERKZEUG_RUN_MAIN)==true:
#应用程序未处于调试模式,或者我们处于重载过程中
use_reloader=False
to your call to app.run
- that is: app.run(debug=True, use_reloader=False)
WERKZEUG_RUN_MAIN
in the environment:if os.environ.get("WERKZEUG_RUN_MAIN") == "true":
# The reloader has already run - do what you want to do here
if not app.debug or os.environ.get("WERKZEUG_RUN_MAIN") == "true":
# The app is not in debug mode or we are in the reloaded process