我正在尝试使用 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
beat 进程再次启动,我可以看到消息已成功传递给 worker,但无法弄清楚如何“注册”任务以便由 worker 处理它。
最佳答案
我能够通过将应用程序从 facebook-call
重命名为与文件名 FacebookAPICall
一致来解决问题
之前:
app = Celery('facebook-call', broker='amqp://localhost//'
之后:
app = Celery('FacebookAPICall', broker='amqp://localhost//'
通过阅读 Celery 文档,我不完全理解为什么应用程序的名称也必须是 .py
文件的名称,但这似乎可以解决问题。
关于python - 如何修复接收未注册任务错误 - Celery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55427015/