任务状态和django-celery [英] Task state and django-celery

查看:105
本文介绍了任务状态和django-celery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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")

我使用django shell运行任务:

I use django shell to run task:

python manage.py shell

r = tasks.TestTask().delay()

从celery日志中,我看到任务已执行:

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'

即使任务执行良好。

为什么会这样?

推荐答案

您正在使用什么版本的芹菜?我注意到我参加这个聚会真的很晚,但以防万一将来对某人有帮助。如果任务设置为ignore_result(在最新版本中默认为默认),它将停留在PENDING而不是SUCCESS。

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.

此处的文档,

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

您可以自己查看

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

**另外,另一个注意事项,即使您将ignore_result设置为true,也可以始终像这样手动更新状态,

** 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})

进度条的性质-也可以在celery的文档页面上找到。

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

这篇关于任务状态和django-celery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆