首页 > 解决方案 > 如何修复接收未注册任务错误 - Celery

问题描述

我正在尝试使用 Celery (4.2.0) 和 RabbitMQ (3.7.14) 在使用 Ubuntu 16.04 的 Azure VM 上运行 Python 3.7.2 建立一个定期任务。我能够启动节拍和工作人员,并看到消息从节拍发送到工作人员,但此时我遇到了这样的错误

[2019-03-29 21:35:00,081: ERROR/MainProcess] Received 
unregistered task of type 'facebook-call.facebook_api'.
The message has been ignored and discarded.

Did you remember to import the module containing this task?
Or maybe you're using relative imports?

我的代码如下:

from celery import Celery
from celery.schedules import crontab

app = Celery('facebook-call', broker='amqp://localhost//')

@app.task
def facebook_api():
    {function here}

app.conf.beat.schedule = {
    'task': 'facebook-call.facebook_api',
    'schedule': crontab(hour=0, minute =0, day='0-6'),
}

我使用包含所有代码的 python 文件的名称来启动节拍和工作进程

celery -A FacebookAPICall beat --loglevel=info
celery -A FacebookAPICall worker --loglevel=info

同样,节拍过程开始,我可以看到消息已成功传递给工作人员,但无法弄清楚如何“注册”任务以便由工作人员处理。

标签: pythonlinuxrabbitmqcelery

解决方案


我能够通过将应用程序重命名facebook-call为与文件名一致来解决问题FacebookAPICall

前: app = Celery('facebook-call', broker='amqp://localhost//'

后: app = Celery('FacebookAPICall', broker='amqp://localhost//'

通过阅读 Celery 文档,我不完全理解为什么应用程序的名称也必须是.py文件的名称,但这似乎可以解决问题。


推荐阅读