Retrieving GroupResult from taskset_id in Celery?

2020-02-26 08:43发布

问题:

I am starting a set of celery tasks by using celery group as described in the official documentation

I am also storing the group (taskset) id into a db, in order to poll celery for the taskset state.

job = group([
        single_test.s(1, 1),
        single_test.s(1, 2),
        single_test.s(1, 3),
    ])

result = job.apply_async()

test_set = MyTestSet()
test_set.taskset_id = result.id

# store test_set into DB

Is there a way to obtain a GroupResult object (i.e. my result) starting from the taskset id? Something like what is done in this question, but working with celery groups.

I already tried doing:

r = GroupResult(taskset_id)

but it does not work, as r.results() is always empty.

Should I use GroupResult.save() and GroupResult.restore() methods?

回答1:

Yes you have to save the result and then restore it.

job = group([
    single_test.s(1, 1),
    single_test.s(1, 2),
    single_test.s(1, 3),
])
result = job.apply_async()
result.save()

from celery.result import GroupResult
saved_result = GroupResult.restore(result.id)

I had the same issue and after seeing your hint about save/restore eventually figured it out.