Skip to content

Instantly share code, notes, and snippets.

@ask
Forked from lrvick/get_results.py
Created June 22, 2011 17:37
Show Gist options
  • Save ask/1040634 to your computer and use it in GitHub Desktop.
Save ask/1040634 to your computer and use it in GitHub Desktop.
Get async celery results from subtasks
from celery.result import AsyncResult
from celery.execute import send_task
def get_results(queries):
result = send_task('task1',queries)
results = result.get()
#this does not return ids until _after_ all the tasks are complete, for some reason.
while results:
#pop first off queue, this will shorten the list and eventually break out of while
first_id = results.pop(0)
r = AsyncResult(first_id)
if not r.ready():
results.append(first_id) #add it back to the bottom o fthe queue
else:
out = r.get()
if out: print out
get_results(['a','b','c'])
from celery.decorators import task
@task
def task1(queries):
task_ids = []
for query in queries:
result = task2.delay(query)
task_ids.extend(result.get())
return task_ids
@task
def task2(query):
task_ids = []
i = 0
while i < 20:
i += 1
result = task3.delay()
task_ids.append(result.task_id)
return task_ids
@task
def task3()
time.sleep(2)
return 'done'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment