我使用 django-celery 并有这样的任务:
class TestTask(Task):
name = "enabler.test_task"
def run(self, **kw):
debug_log("begin test task")
time.sleep(5)
debug_log("end test task")
def on_success(self, retval, task_id, args, kwargs):
debug_log("on success")
def on_failure(self, retval, task_id, args, kwargs):
debug_log("on failure")
我使用 django shell 来运行任务:
python manage.py shell
r = tasks.TestTask().delay()
从芹菜日志我看到该任务已执行:
[2012-01-16 08:13:29,362: INFO/MainProcess] Got task from broker: enabler.test_task[e2360811-d003-45bc-bbf8-c6fd5692c32c]
[2012-01-16 08:13:29,390: DEBUG/PoolWorker-3] begin test task
[2012-01-16 08:13:34,389: DEBUG/PoolWorker-3] end test task
[2012-01-16 08:13:34,390: DEBUG/PoolWorker-3] on success
[2012-01-16 08:13:34,390: INFO/MainProcess] Task enabler.test_task[e2360811-d003-45bc-bbf8-c6fd5692c32c] succeeded in 5.00004410744s: None
但是,当我从地狱检查任务状态时,我总是得到 PENDING:
>>> r = tasks.TestTask().delay()
>>> r
<AsyncResult: e2360811-d003-45bc-bbf8-c6fd5692c32c>
>>> r.state
'PENDING'
>>> r.state
'PENDING'
>>> r.state
'PENDING'
>>> r.state
'PENDING'
即使任务执行得很好。
为什么会发生这种情况?