multithreading - flask application with background threads -
i creating flask application, 1 request need run long running job not required wait on ui. create thread , send message ui. thread calculate , update database. but, ui see message upon submit. below implementation, running thread , sending output ui not prefer. how can run thread in background?
@app.route('/somejob') def index(): t1 = threading.thread(target=long_running_job) t1.start() return 'scheduled job' def long_running_job #some long running processing here
how can make thread t1 run background , send message in return?
the best thing stuff use message broker. there excellent software in python world meant doing this:
- celery (http://www.celeryproject.org/), and
- rq (http://python-rq.org/).
both excellent choices.
it's never idea spawn thread way you're doing it, can cause issues processing incoming requests, among other things.
if take @ celery or rq getting started guides, they'll walk through doing proper way!
Comments
Post a Comment