首页 > python flask框架 app.debug=True时,启动脚本被执行了2次?

python flask框架 app.debug=True时,启动脚本被执行了2次?

我的flask 启动脚本run.py如下

import os

from test import app

print "test"
if __name__ == "__main__":
    app.run()

python run.py执行后
发现输出两次test

如下:

test
 * Running on xxxxxx
test

但是app.debug关闭之后,只输出一次,即

test
 * Running on xxxxx

这是什么原理呢?


原因是DEBUG模式下flask开多一个线程来监视项目的变化。

The first thing it does is start the main function in a new thread so it can monitor >the source files and restart the thread when they change.

参考自这篇文章http://stackoverflow.com/questions/9276078/whats-the-right-approach-for-calling-functions-after-a-flask-app-is-run。

如果你想要避免加载两次,应该设置app.run(debug=True, use_reloader=False)

【热门文章】
【热门文章】