python - Flask 启动的时候为什么会执行两遍这部分代码?
本文介绍了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
的一个问题?
解决方案
简单的搜一下:
Why does running the Flask dev server run itself twice?
这篇关于python - Flask 启动的时候为什么会执行两遍这部分代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文