Created
June 22, 2011 17:02
-
-
Save lrvick/1040562 to your computer and use it in GitHub Desktop.
Get async celery results from subtasks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from celery.decorators import task | |
@task | |
def task1(queries): | |
task_ids = [] | |
taskset = TaskSet(task2.subtask((query, )) for query in queries).apply_async() | |
results = taskset.join() | |
@task | |
def task2(query): | |
task_ids = [r.task_id for r in TaskSet(task3.subtask() for _ in xrange(20)).apply_async()] | |
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