[英]Flask app deployed on Heroku threading problem
I wrote a Flask application that needs a secondary thread to display a video stream in the browser.我写了一个 Flask 应用程序,它需要一个辅助线程来在浏览器中显示视频 stream。 Here is the way the thread is created:
这是创建线程的方式:
if __name__ == '__main__':
# # start a thread that will perform motion detection
t = threading.Thread(target=prepare_video_stream)
t.daemon = True
t.start()
print(t.is_alive())
app.run(debug=True, threaded=True, use_reloader=False)
Everything works fine when a run my application with the built-in flask server, but after deploying to heroku with gunicorn the thread doesn't seem to start.当使用内置的 flask 服务器运行我的应用程序时,一切正常,但是在使用 gunicorn 部署到 heroku 后,线程似乎没有启动。 Here is my Procfile:
这是我的Procfile:
web: gunicorn app:app
How can I make the thread run?如何使线程运行? Am I missing something?
我错过了什么吗?
I found a solution to this我找到了解决方案
The key is to run your thread in @app.before _first_request
instead of the name locked __main__
关键是在
@app.before _first_request
中运行你的线程,而不是锁定名称__main__
@app.before_first_request
def thread_start():
# # start a thread that will perform motion detection
t = threading.Thread(target=prepare_video_stream)
t.daemon = True
t.start()
print(t.is_alive())
if __name__ == '__main__':
app.run(debug=True, threaded=True, use_reloader=False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.