2016-11-29 96 views
0

我想檢查我的任務組是否已準備就緒。如果我在芹菜任務中執行它,它工作正常:Django和Celery - 檢查GroupResult是否準備就緒時出現錯誤

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' 

此外,當我把它拍芹菜任務裏面會產生錯誤。

我使用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' 

回答

0

已解決。我有一個沒有任務的團隊。