本文介绍了在代码或"flask run"命令中指定Flask重新加载监视列表(extra_files)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些Flask应用程序,可以通过设置 FLASK_APP 环境变量并调用 flask run 来运行.由于还设置了 FLASK_DEBUG ,因此只要我更改一些代码,应用程序就可以方便地重新加载.

但不是所有代码.还有其他文件,即我使用 加载的配置文件app.config.from_pyfile ,我也希望该应用程序也可以观看,因此如果我更改了这些内容,它将重新加载.我该怎么办?

如果我从代码中调用它,我知道我可以在内置的Werkzeug服务器中指定 extra_files .但是正如我提到的,我实际上是在使用内置的 flask run 命令.我在这个项目中有多个应用程序,因此能够选择使用 FLASK_APP 运行的应用程序被证明很方便...除了似乎没有一种指定 extra_files .我可以编写一些能同时完成这两个操作的引导程序代码,但我更喜欢使用一些内置的方式(如果存在).

如果我可以简单地在应用程序本身中指定文件,然后在加载文件时将其添加到监视列表,那将特别方便.不幸的是,即使 extra_files 似乎不是 App 对象的成员,即使它是 app.run()中的参数./p>

我无法想象这是一个不常见的用例.Flask是否提供一种我想要做的方式?

解决方案

我刚刚在 manage.py 中尝试了以下命令:

  @ manager.option('-w','--wsgi_server',dest ='server',default ='flask',help ='[flask | gevent | tornado]')@ manager.option('-p','--port',dest ='port',默认= 5000,help ='收听端口')@ manager.option('-d','--debug',dest ='debug',action ="store_true",default = False,help ='显示调试信息')def运行(服务器,端口,调试):app.connexion_app.run(port = int(端口),服务器=服务器,debug = debug,extra_files = ['./proj/oauth2/swagger.yaml','./proj/api/swagger.yaml',]) 

extra_files 似乎没问题:

  *调试器处于活动状态!*调试器PIN:336-632-033*检测到'< -snip->/proj/api/swagger.yaml'中的更改,正在重新加载 

I have some Flask applications which I run by setting the FLASK_APP environment variable and invoking flask run. Since FLASK_DEBUG is also set, the application conveniently reloads whenever I change some code.

But not all code. There are other files, namely config files that I load with app.config.from_pyfile, that I want the app to watch too, so it reloads if I change those. How can I do that?

I know I can specify extra_files in the built-in Werkzeug server if I invoke it from code. But as I mentioned, I'm actually using the built-in flask run command. I have multiple apps in this project, so being able to choose which one to run with FLASK_APP has proven convenient... except that there doesn't seem to be a way to specify extra_files. I could write some bootstrap code that does both, but I'd prefer to use some built-in way, if it exists.

What would be especially convenient is if I could simply specify the files in the app itself, adding them to a watch list as I load them. Sadly, extra_files doesn't seem to be a member of the App object, even though it's a parameter in app.run().

I can't imagine this being an uncommon use case. Does Flask provide a way to do what I want?

解决方案

I've just tried the following command in manage.py:

@manager.option('-w', '--wsgi_server', dest='server', default='flask',
            help='[flask|gevent|tornado]')
@manager.option('-p', '--port', dest='port', default=5000,
            help='Port to listen')
@manager.option('-d', '--debug', dest='debug', action="store_true", default=False,
            help='Show debugging information')
def run(server, port, debug):
    app.connexion_app.run(
        port=int(port),
        server=server,
        debug=debug,
        extra_files=[
            './proj/oauth2/swagger.yaml',
            './proj/api/swagger.yaml',
        ])

and extra_files seems to be picked up fine:

 * Debugger is active!
 * Debugger PIN: 336-632-033
 * Detected change in '<-snip->/proj/api/swagger.yaml', reloading

这篇关于在代码或"flask run"命令中指定Flask重新加载监视列表(extra_files)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 17:28