Python Flask,使用重新加载器重新启动:这是什么意思
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25855997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Flask, Restarting with reloader: What does that mean
提问by konquestor
I am trying to build y first webservice using Python Flask. I am not able to understand what does it mean for Flask to emit out Restarting with reloader, every time i run my app.
我正在尝试使用 Python Flask 构建第一个 Web 服务。我无法理解每次运行我的应用程序时 Flask 发出 Restarting with reloader 是什么意思。
This is my code.
这是我的代码。
#!venv/bin/python
from flask import Flask
from flask import request
def buildCache():
print 'Hello World'
buildCache()
app = Flask(__name__)
@app.route('/search')
def index():
query = request.args.get('query','', type=str);
return query
if __name__ == '__main__':
app.run(debug = True)
when i run it
当我运行它时
venv/bin/python ./app.py
Hello World
* Running on http://127.0.0.1:5000/
* Restarting with reloader
Hello World
I dont understand why buildCache method is being called twice? It seems to be related to "Restarting with the reoloader', what does that mean? How do i make sure that buildCache is only executed once, before the server starts.
我不明白为什么 buildCache 方法被调用两次?它似乎与“使用 reoloader 重新启动”有关,这是什么意思?如何确保 buildCache 在服务器启动之前只执行一次。
回答by Muntaser Ahmed
This "reloads" the code whenever you make a change so that you don't have to manually restart the app to see changes. It is quite useful when you're making frequent changes.
每当您进行更改时,这都会“重新加载”代码,这样您就不必手动重新启动应用程序即可查看更改。当您进行频繁更改时,它非常有用。
You can turn off reloading by setting the debug parameter to False.
您可以通过将调试参数设置为 False 来关闭重新加载。
app.run(debug=False)
"[If debug=True] the debugger will kick in when an unhandled exception occurs and the integrated server will automatically reload the application if changes in the code are detected."
“[如果debug=True] 调试器将在发生未处理的异常时启动,如果检测到代码更改,集成服务器将自动重新加载应用程序。”
Source: http://flask.pocoo.org/docs/0.10/api/#flask.Flask.debug
回答by Slava Bacherikov
From flask documentation:
从烧瓶文档:
If you enable debug support the server will reload itself on code changes, and it will also provide you with a helpful debugger if things go wrong.
如果您启用调试支持,服务器将在代码更改时重新加载自己,并且如果出现问题,它还将为您提供有用的调试器。
See this.
看到这个。
Try start application and then do touch app.py, if debug mode will be enable server will reload application.
尝试启动应用程序,然后执行touch app.py,如果启用调试模式,服务器将重新加载应用程序。

