【问题标题】:Django with Celery: Getting the status from an undergoing taskDjango with Celery:从正在进行的任务中获取状态
【发布时间】:2015-05-25 17:51:35
【问题描述】:

我目前正在用 Celery 测试 Django。

使用 crontab 设置定期任务以运行以下任务添加:

from __future__ import absolute_import

from celery import task
import time

@task
def add(x, y):
    print "Start : %s" % time.ctime()
    time.sleep(120)
    print "End : %s" % time.ctime()
    return x + y

型号:

from django.db import models
from djcelery.models import CrontabSchedule
from djcelery.models import TaskState

class Manager(models.Model):
    cron = models.ForeignKey(CrontabSchedule)

    def app_status(self):
        return self.cron.schedule.app.tasks

    app_status.admin_order_field = 'app status'

现在我希望能够访问当前或上一个任务的状态。使用 Django shell,我可以获得以下内容:

>>> import django
>>> from extractionapp.models import Manager
>>> Manager.objects.all()[0].app_status()
{'celery.chain': <@task: celery.chain of proj:0x1022f1a90>, 'celery.chord': <@task: celery.chord of proj:0x1022f1a90>, 'proj.celery.debug_task': <@task: proj.celery.debug_task of proj:0x1022f1a90>, 'celery.chunks': <@task: celery.chunks of proj:0x1022f1a90>, 'celery.chord_unlock': <@task: celery.chord_unlock of proj:0x1022f1a90>, 'celery.group': <@task: celery.group of proj:0x1022f1a90>, 'celery.backend_cleanup': <@task: celery.backend_cleanup of proj:0x1022f1a90>, 'celery.map': <@task: celery.map of proj:0x1022f1a90>, 'celery.starmap': <@task: celery.starmap of proj:0x1022f1a90>}

但是,我无法从此访问添加任务,也无法访问其状态。 按名称调用任务也不起作用:

Manager.objects.all()[0].app_status()['extractionapp.tasks.add'] 回溯(最近一次通话最后): 文件“”,第 1 行,在 文件“/Users/antoinebrunel/seo/lib/python2.7/site-packages/celery/app/registry.py”,第 26 行,缺少 提高 self.NotRegistered(key) 未注册:'extractionapp.tasks.add'

那么我怎样才能访问分配给 cron 的当前任务的状态呢?我可以通过主页 › Djcelery › 任务从管理员那里看到它,但我如何从代码中获得它?

非常感谢!

【问题讨论】:

    标签: django celery django-celery celery-task djcelery


    【解决方案1】:

    来自 Django 外壳:

    >>> from celery import Celery
    >>> app = Celery('proj')
    >>> i = app.control.inspect()
    >>> i.active()
    {u'celery@HeyHeyHey': [{u'args': u'[2, 2]', u'time_start': 448519.58944676, u'name': u'extractionapp.tasks.add', u'delivery_info': {u'priority': None, u'redelivered': False, u'routing_key': u'celery', u'exchange': u'celery'}, u'hostname': u'celery@HeyHeyHey', u'acknowledged': True, u'kwargs': u'{}', u'id': u'05fa4347-a222-45f3-9ee0-f3c261a21a24', u'worker_pid': 23999}]}
    

    来源:http://celery.readthedocs.org/en/latest/userguide/workers.html#dump-of-currently-executing-tasks

    【讨论】:

      猜你喜欢
      • 2012-02-11
      • 1970-01-01
      • 1970-01-01
      • 2015-11-19
      • 2013-09-09
      • 2017-06-16
      • 2014-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多