【问题标题】:Flask app deployed on Heroku threading problemFlask 应用程序部署在 Heroku 线程问题上
【发布时间】:2020-09-07 01:42:54
【问题描述】:

我编写了一个 Flask 应用程序,它需要一个辅助线程来在浏览器中显示视频流。 以下是线程的创建方式:

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)

当使用内置的烧瓶服务器运行我的应用程序时一切正常,但是在使用 gunicorn 部署到 heroku 后,线程似乎没有启动。 这是我的 Procfile:

web: gunicorn app:app

如何使线程运行?我错过了什么吗?

【问题讨论】:

    标签: python flask heroku python-multithreading


    【解决方案1】:

    我找到了solution to this

    关键是在@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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-18
      • 2017-12-05
      • 2014-09-02
      • 1970-01-01
      • 1970-01-01
      • 2018-11-19
      • 2021-09-23
      相关资源
      最近更新 更多