Django和Celery - 检查GroupResult是否准备就绪的关键错误

时间:2016-11-29 15:32:47

标签: python django redis celery

我想检查我的任务组是否准备就绪。如果我在celery任务中执行它,它可以正常工作:

from time import sleep

from celery import task, group
from celery.result import GroupResult

@task
def a():
    sleep(10)


@task
def b():
    c = group(a.si())()
    c.save()
    saved_result = GroupResult.restore(c.id)
    print saved_result.ready()


b.apply_async()

但是,它在我的Django视图中不起作用。

task_result = GroupResult.restore(my_hash)
print type(task_result)
if task_result.ready(): # this throws an error
     print 'ready!'

错误:

    if task_result.ready():
  File "/home/kam/project1_env/local/lib/python2.7/site-packages/celery/result.py", line 259, in ready
    return self.state in self.backend.READY_STATES
  File "/home/kam/project1_env/local/lib/python2.7/site-packages/celery/result.py", line 396, in state
    return self._get_task_meta()['status']
  File "/home/kam/project1_env/local/lib/python2.7/site-packages/celery/result.py", line 341, in _get_task_meta
    return self._maybe_set_cache(self.backend.get_task_meta(self.id))
  File "/home/kam/project1_env/local/lib/python2.7/site-packages/celery/result.py", line 332, in _maybe_set_cache
    state = meta['status']
KeyError: 'status'

当我在celery beat任务中调用它时,它会引发错误。

我使用Redis作为后端。我的芹菜设置:

BROKER_URL = 'redis://127.0.0.1:6379/1'
CELERY_RESULT_BACKEND = 'redis://127.0.0.1:6379/1'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'

1 个答案:

答案 0 :(得分:0)

解决。我有一个没有任务的小组。