flask application with background threads

前端 未结 4 994
北恋
北恋 2020-12-13 01:07

I am creating a flask application, for one request I need to run some long running job which is not required to wait on the UI. I will create a thread and send a message to

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-13 01:23

    Check out Flask-Executor which uses concurrent.futures in the background and makes your life very easy.

    from flask_executor import Executor
    
    executor = Executor(app)
    
    @app.route('/someJob')
    def index():
        executor.submit(long_running_job)
        return 'Scheduled a job'
    
    def long_running_job
        #some long running processing here
    

    This not only runs jobs in the background but gives them access to the app context. It also provides a way to store jobs so users can check back in to get statuses.

提交回复
热议问题