这是另一个烧瓶开发服务器重新加载器问题。有一百万个问题询问为什么它会两次加载所有内容,而这不是其中之一。我知道它两次加载所有内容,我的问题涉及处理这个现实,我还没有找到我认为解决我正在尝试做的事情的答案。
我的问题是,如何在退出时清除所有应用程序对象?
我目前的方法如下所示。在这个例子中,我使用 atexit 函数运行我的清理代码。
from flask import Flask
app = Flask(__name__)
print("start_app_id: ", '{}'.format(id(app)))
import atexit
@atexit.register
def shutdown():
print("AtExit_app_id: ", '{}'.format(id(app)))
#do some cleanup on the app object here
if __name__ == "__main__":
import os
if os.environ.get('WERKZEUG_RUN_MAIN') == "true":
print("reloaded_main_app_id: ", '{}'.format(id(app)))
else:
print("first_main_app_id: ", '{}'.format(id(app)))
app.run(host='0.0.0.0', debug=True)
Run Code Online (Sandbox Code Playgroud)
这段代码的输出如下:
start_app_id: 140521561348864
first_main_app_id: 140521561348864
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
start_app_id: 140105598483312
reloaded_main_app_id: 140105598483312
* Debugger is active!
* Debugger pin code: xxx-xxx-xxx …Run Code Online (Sandbox Code Playgroud)