我的代码如下:
app = Flask(__name__)
def target():
while True:
time.sleep(10)
print('ok')
if __name__ == '__main__':
thread(target = target, args = ()).start()
app.run()
为什么目标函数运行两次? (我的意思是,每10秒输出为2 'ok'
)
我试图在@Gabip上发表评论,但似乎标记的评论确实受到限制。
这是一个完整的示例,即使我的计算机上没有线程,它也会打印两次。
from flask import Flask
app = Flask(__name__)
@app.route('/', methods=['GET'])
def index():
return 'Hello World'
if __name__ == '__main__':
print('This gets printd twice')
app.run(host='127.0.0.1', port=8080, debug=True)
输出:
$ python test.py
This gets printd twice
* Serving Flask app "test" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:8080/ (Press CTRL+C to quit)
* Restarting with stat
This gets printd twice
* Debugger is active!
* Dbugger PIN: 977-268-905