Task state and django-celery

假如想象 提交于 2020-01-14 07:16:05

问题


I use django-celery and have task like this:

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")

I use django shell to run task:

python manage.py shell

r = tasks.TestTask().delay()

From celery log I see that task is executed:

[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

However when I check task state from hell I always got PENDING:

>>> r = tasks.TestTask().delay()
>>> r
<AsyncResult: e2360811-d003-45bc-bbf8-c6fd5692c32c>
>>> r.state
'PENDING'
>>> r.state
'PENDING'
>>> r.state
'PENDING'
>>> r.state
'PENDING'

even though task is well executed.

Why this happens?


回答1:


What version of celery are you using? I notice I'm really late to this party but in case this helps someone in the future. If the task is set to ignore_result (which it is by default in the latest version) it will stay at PENDING and not go to SUCCESS.

Their documentation here,

@celery.task(ignore_result=True)
def mytask(...)
    something()

You can view it yourself here, if you have any other questions let me know.

** Also another note, even if you have ignore_result set to true you can always manually update the state like so,

from celery import current_task
current_task.update_state(state='PROGRESS', meta={'description': 'Doing some task', 'current': 59, 'tota': 73})

Something of that nature for progress bars -- also found on celery's documentation page.



来源:https://stackoverflow.com/questions/8876863/task-state-and-django-celery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!