芹菜计划列表返回无

2022-01-11 00:00:00 python celery rabbitmq

问题描述

我对 Celery 还很陌生,我一直在尝试设置一个简单的脚本来安排和取消安排任务.但是我觉得我遇到了一个奇怪的问题.我有以下设置

I'm fairly new to Celery and I've been attempting setup a simple script to schedule and unschedule tasks. However I feel like I'm running into a weird issue. I have the following setup

from celery import Celery
app = Celery('celery_test',
             broker='amqp://',
             backend='amqp')

@app.task
def add(x, y):
    return x + y

我很好地启动了我的 celery 服务器,并且可以添加任务.现在,当我想获取活动任务列表时,事情似乎变得很奇怪.当我使用检查来获取计划任务列表时,它只工作一次,然后每次都返回无.

I start up my celery server just fine and can add tasks. Now when I want to get a list of active tasks things seem to get weird. When I goto use inspect to get a list of scheduled tasks it works exactly once then returns None every time afterwards.

>>> i = app.control.inspect()
>>> print i.scheduled()
{u'celery@mymachine': []}
>>> print i.scheduled()
None
>>>

无论我是否添加任务都会发生这种情况.我想找到一种方法来始终如一地从我的芹菜队列中返回任务列表.我想这样做,这样我就可以找到以前排队的任务,撤销它,然后重新安排它.我觉得我在这里缺少一些基本的东西.

This happens whether I add tasks or not. I want to find a way to consistently return a list of tasks from my celery queue. I want to do this so I can find a previously queued task, revoke it, and reschedule it. I feel like I'm missing something basic here.


解决方案

要重复调用以获取队列中的任务列表,您必须创建 Celery 对象的新实例.我试图通过调试调用 ./manage.py celery inspect scheduled 执行的代码来弄清楚为什么有必要,但没有任何运气.也许有人会有更多的经验,并在这个答案中添加一些额外的信息.

To repeat call to get list of tasks in queues you have to create new instance of Celery object. I was trying to figure out why it's necessary by debugging code executed by calling ./manage.py celery inspect scheduled but without any luck. Maybe someone will have more experience with that and add some additional informations to this answer.

试试这个简单的片段来检查计划任务列表:

Try this simple snippet for checking list of scheduled tasks:

from celery import Celery

def inspect(method):
    app = Celery('app', broker='amqp://')
    return getattr(app.control.inspect(), method)()

print inspect('scheduled')
print inspect('active')

相关文章