先放上一段最简单的代码:
#!/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 的一个问题?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
简单的搜一下:
Why does running the Flask dev server run itself twice?