Python Flask,用reloader重启:这是什么意思 [英] Python Flask, Restarting with reloader: What does that mean

查看:2926
本文介绍了Python Flask,用reloader重启:这是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python Flask构建第一个webservice。我无法理解Flask发出的意味着每次运行我的应用程序时都重新启动重新启动。



这是我的代码。

 #!venv / bin / python $ b $从瓶子导入Flask 
从瓶子导入请求


def buildCache():
print'Hello World'

uildCache()

app = Flask(__ name__)


@ app.route('/ search')
def index():
query = request.args.get('query','',type = str);
返回查询

$ b如果__name__ =='__main__':
app.run(debug = True)

当我运行时

  venv / bin / python ./app.py 
Hello World
*运行于http://127.0.0.1:5000/
*重新启动
Hello World

我不明白为什么buildCache方法被调用两次?这似乎与重新启动reloader有关,这是什么意思?我如何确保buildCache只在服务器启动前执行一次。

解决方案

无论何时进行更改,都会重新加载代码,以便您不必手动重新启动应用程序即可查看更改。 / p>

您可以通过将debug参数设置为False来关闭重新加载。

  app.run(debug = False)


$ b 如果 debug = True ]当未处理的异常发生时,调试器将启动,如果检测到代码中的变化,集成服务器将自动重新加载应用程序。

来源: http://flask.pocoo.org/docs/0.10/api/#flask .Flask.debug


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.

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.

解决方案

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.

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."

Source: http://flask.pocoo.org/docs/0.10/api/#flask.Flask.debug

这篇关于Python Flask,用reloader重启:这是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆