我有一个 app.py flask 应用程序,我想为其启用自动重新加载。这是入口点:
APP = Flask(__name__)
APP.config.from_object(os.environ['APP_SETTINGS'])
# a lot of configurations ommited
if __name__ == "__main__":
APP.run(debug=True, port=5005)
当我运行应用程序时,我在终端中得到了这个:
/Users/George/myproject/venv/bin/python /Users/George/myproject/app.py
* Running on http://127.0.0.1:5005/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger pin code: 338-825-330
当我修改我的一个 Controller 时,它发现发生了变化:
* Detected change in '/Users/George/myproject/web_endpoints/user.py', reloading
但更改不会发生,如果我再做一次更改,它永远不会被拾取(不会在终端中报告)。
最佳答案
不建议 Flask 将 app.run() 与自动重新加载一起使用,因为它受到严重支持
这是Flask源码中的注释
def run(self, host=None, port=None, debug=None, load_dotenv=True, **options):
"""Runs the application on a local development server.
...
If you want to run the application in debug mode, but disable the
code execution on the interactive debugger, you can pass
``use_evalex=False`` as parameter. This will keep the debugger's
traceback screen active, but disable code execution.
It is not recommended to use this function for development with
automatic reloading as this is badly supported. Instead you should
be using the :command:`flask` command line script's ``run`` support.
...
"""
pass
但是你可以像这样强制使用重新加载器:
if __name__ == '__main__':
app.run(host=config.HOST, port=config.PORT, debug=True)
注意,app.run()
必须用 if __name__ == '__main__'
包裹。
关于python - Flask autoreload 不重新加载或拾取更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45089465/