简体   繁体   English

Flask 应用程序部署在 Heroku 线程问题

[英]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.

相关问题 在已部署的 Flask 应用程序 (Heroku) 上使用 OpenCV - Use OpenCV on deployed Flask app (Heroku) 我在heroku上部署了flask应用程序,它立即崩溃了 - I deployed a flask app on heroku , it was crashed immediately 访问 Heroku 部署的 Flask 应用程序时出错 - Errors when visiting Heroku-deployed Flask app 部署在 heroku 上时,Flask 应用程序出现 H10 错误 - H10 Error in Flask App when deployed on heroku 当烧瓶应用程序部署到Heroku时,为什么不导入flask-bootstrap base.html模板? - Why won't the flask app import the flask-bootstrap base.html template when deployed to Heroku? Heroku 中的目录问题与 Flask - Problem with directory in Heroku with Flask Flask App通过Heroku部署时由于Error = H10而导致应用程序错误 - Flask App results in Application Error when deployed through Heroku due to Error=H10 用于部署在heroku上的python flask Web应用程序。 前端如何知道动态后备服务器端口? - for a python flask web app deployed on heroku. how front end know dynamic backed server port? Flask-Pymongo应用程序部署到Heroku时出现服务器错误……我缺少什么? - Server error on Flask-Pymongo app deployed to Heroku… what am I missing? 部署在heroku中的flask应用程序数据库数据会在一段时间后自动删除 - flask app database data is automatically removed after some time which is deployed in heroku
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM