简体   繁体   中英

Task state and django-celery

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM