Reputation: 1499
I have a django webserver, and a form in which the user enters information. Everytime the form information changes I update the model in my database, and at a certain point when something validates I will create a long running task in celery to get my results even before the user clicked next.
I am using Django Celery with RabbitMQ as broker, and my question is what is the most appropriate way of IN CASE the task is still not finished to just lock the response thread in django until the task is state.SUCCESSFUL I tried using the AsyncResult.get method for that, but it just locks the thread for a real long time and then gives me the result. IE It's not instant, does anyone have an idea how to solve this?
Upvotes: 10
Views: 13244
Reputation: 43832
You can just wait until the result is ready()
.
from time import sleep
result = some_task.apply_async(args=myargs)
while not result.ready():
sleep(0.5)
result_output = result.get()
It appears there is also a wait()
, so you could just use that. The following should is basically doing the same thing as the code above.
result = some_task.apply_async(args=myargs)
result_output = result.wait(timeout=None, interval=0.5)
Upvotes: 19
Reputation: 11396
one way to accomplish that would be to have the results waiting in redis, and get them using a blocking pop operation using some unique value like session id, note its timeout capability.
Upvotes: 1