【问题标题】:Flask autoreload not reloading or picking up changes烧瓶自动重新加载不重新加载或拾取更改
【发布时间】:2017-12-18 18:11:19
【问题描述】:

我有一个 app.py 烧瓶应用程序,我想为其启用自动重新加载。这是入口点:

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

当我修改我的一个控制器时,它会发现发生了变化:

 * Detected change in '/Users/George/myproject/web_endpoints/user.py', reloading

但没有发生更改,如果我进行另一项更改,它永远不会被拾取(未在终端中报告)。

【问题讨论】:

  • 同样的问题,有人找到解决方案了吗?
  • 我发现你最好在启动和任何迭代循环结束时标记文件模式时间戳,然后在时间戳改变时退出。运行外部循环以从头开始备份。只是一个注释“我也是” - 我刚刚又看到了这个问题。使用 apschedule,应用程序将在重新加载时重复调度。添加了“atexit.register(lambda: scheduler.shutdown())”但没有帮助。

标签: python flask


【解决方案1】:

不建议 Flask 使用 app.run() 进行自动重新加载,因为它的支持很差

这是 Flask 源代码中的 cmets

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      

但是你可以像这样强制使用重新加载器:

app.run(debug=True, use_reloader=True)

【讨论】:

  • 这并不能真正提供答案。
  • 可以,您可以添加use_reloader=True,它会成功的:)
猜你喜欢
  • 2018-12-25
  • 2019-06-22
  • 2014-11-22
  • 1970-01-01
  • 2022-10-15
  • 2017-11-04
  • 2020-12-14
  • 2013-04-07
相关资源
最近更新 更多