4π
β
The task.delay() is asynchronous. BTW, the whole AMQP thing is about making tasks asynchronous. If you want synchronous behavior, what is the point of using celery?
from celery.decorators import task
@task()
def add(x, y, results):
results.append(x + y)
------------8<-------------
from core.tasks import add
results = []
for i in range(100):
add.delay(i, i, results)
Wait a few seconds before printing (while the consumers do their work) and be aware that results may came up out of order.
Method task.delay will return a celery.result.AsyncResult instance. Before using AsyncResult.result you should check for AsyncResult.state == SUCCESS.
Your final print loop could be:
for result in results:
while not result.state.ready():
time.sleep(secs)
if result.state == u'SUCCESS':
print result.result
else:
print "Something Rotten in the State of Denmark..."
This is (almost) the same as:
for result in results:
print result.get()
Look at TaskSets, they are better than storing results in a list.
In practice people store results in the database and place the wait loop in the client that will then hammer the server via AJAX every few seconds until completion.
π€Paulo Scardine
Source:stackexchange.com