python - Django 和 Celery - 检查 GroupResult 是否准备就绪时出现关键错误

标签 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 View 中不起作用。

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 作为后端。我的 celery 设置:

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'

最佳答案

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

关于python - Django 和 Celery - 检查 GroupResult 是否准备就绪时出现关键错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40869712/

相关文章:

python - 如何在 pyobjc 中生成线程

python - 如何修复 : RuntimeError: size mismatch in pyTorch

python - 将元素添加到python pyspark中的列表列表

去年同一周 Django 查询

node.js - socket.io 如何跨多个服务器发送消息?

python - 如何使用 Pyramid 和烧杯的 cookie session 来正确存储 session_id?

python - Django中抽象模型类与多表继承的结合

django - 对于带有选项的 django 模型字段,如何有效地获取数据库中使用的选项列表?

java - 如何在Spark Streaming中使用redis

ruby-on-rails - Rails 应用程序中的默认 Sidekiq Redis 配置