python - Flask 启动的时候为什么会执行两遍这部分代码?
PHP中文网
PHP中文网 2017-04-18 09:50:01
0
1
229

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

#!/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中文网

认证高级PHP讲师

répondre à tous(1)
阿神

Recherche simple :

Pourquoi l'exécution du serveur de développement Flask s'exécute-t-elle deux fois ?

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!