Flask自动重载不会重新加载或获取更改

时间:2017-07-13 19:28:49

标签: python flask

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

input("Press Enter to begin...")
card_number = input("Enter your card number... ")


f = open(card_number + ".txt","r")
lines = f.readlines()

x = lines[1]
print(x)

当我运行应用程序时,我会在终端中找到它:

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

但是改变没有发生,如果我做了另一次改变,它永远不会被接收(不在终端中报告)。

1 个答案:

答案 0 :(得分:0)

不建议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      

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

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