Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 419
lstipakov Avatar asked Jan 16 '12 07:01

lstipakov


People also ask

Should I use Celery with Django?

Celery makes it easier to implement the task queues for many workers in a Django application.

How do I get Celery status?

By default, Celery does not record a "running" state. When task_track_started is False , which is the default, the state show is PENDING even though the task has started. If you set task_track_started to True , then the state will be STARTED .


1 Answers

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.

like image 179
Skylude Avatar answered Sep 18 '22 05:09

Skylude