首页  >  问答  >  正文

python - Flask 启动的时候为什么会执行两遍这部分代码?

先放上一段最简单的代码:

#!/usr/bin/env python
# coding=utf-8

from flask import Flask

app = Flask(__name__)
print 'hello'

@app.route('/')
def index():
    return 'Hello world.'

if __name__ == '__main__':
    app.run('127.0.0.1', debug=True, port=1234)

运行结果为:

>> python main.py
hello
 * Restarting with stat
hello
 * Debugger is active!
 * Debugger pin code: 913-546-745
 * Running on http://127.0.0.1:1234/ (Press CTRL+C to quit)

这里的会执行两次 print 语句

将这句话改为这样:

if 'x' not in locals().keys():
    print 'hello'
    x = True

还是会输出两次。请问这个是为什么?

ps: 我现在还不能回答,所以在这里补充一下,把 debug 设置为 false 就没有这个问题了……所以说这个是 Flask 的 debug 的一个问题?

PHP中文网PHP中文网2741 天前240

全部回复(1)我来回复

  • 阿神

    阿神2017-04-18 09:52:01

    简单的搜一下:

    Why does running the Flask dev server run itself twice?

    回复
    0
  • 取消回复