[英]KeyError Received unregistered task of type '' on celery while task is registered
I'm a bit new in celery configs.我对 celery 配置有点陌生。
I have a task named myapp.tasks.my_task
for example.例如,我有一个名为
myapp.tasks.my_task
的任务。
I can see myapp.tasks.my_task
in registered tasks of celery when I use celery inspect registered
.当我使用
celery inspect registered
时,我可以在 celery 的注册任务中看到myapp.tasks.my_task
。 doesn't it mean that the task is successfully registered?不是说任务注册成功了吗? why it raises the following error for it:
为什么它会引发以下错误:
KeyError celery.worker.consumer.consumer in on_task_received
Received unregistered task of type 'my_app.tasks.my_task'.
The message has been ignored and discarded.
Did you remember to import the module containing this task?
Or maybe you're using relative imports?
Please see
http://docs.celeryq.org/en/latest/internals/protocol.html
for more information.
The full contents of the message body was:
'[[], {}, {"callbacks": null, "errbacks": null, "chain": null, "chord": null}]' (77b)
there are also other tasks in my_app.tasks
and they work correctly but only this task does not work and gets KeyError : my_app.tasks
中还有其他任务,它们可以正常工作,但只有此任务不起作用并出现KeyError :
@shared_task(queue='celery')
def other_task():
""" WORKS """
...
@shared_task(queue='celery')
def my_task():
""" DOES NOT WORK """
...
It means that Celery can't find the implementation of the task my_app.tasks.my_task
when it was called.意思是Celery在调用时找不到任务
my_app.tasks.my_task
的实现。 Some possible solutions you may want to look at:您可能想查看一些可能的解决方案:
You probably haven't configured correctly either:您可能还没有正确配置:
celery_app.conf.update(imports=['my_app.tasks'])
or celery_app.conf.imports = ['my_app.tasks']
celery_app.conf.update(imports=['my_app.tasks'])
或celery_app.conf.imports = ['my_app.tasks']
celery_app = Celery(..., include=['my_app.tasks'])
celery_app = Celery(..., include=['my_app.tasks'])
Note: If in a Django application, this can be skipped if already using celery_app.autodiscover_tasks()
since the tasks are automatically discovered in the location ./<app_name>/tasks.py
注意:如果在 Django 应用程序中,如果已经使用
celery_app.autodiscover_tasks()
则可以跳过此步骤,因为任务会在位置./<app_name>/tasks.py
中自动发现
If you are only importing my_app
eg celery_app.conf.update(imports=['my_app'])
then I assume you have a file my_app/__init__.py Make sure that inside that file, it imports the task my_app.tasks.my_task
along with my_app.tasks.other_task
so that the celery app knows that such task exists.如果您只导入
my_app
例如celery_app.conf.update(imports=['my_app'])
那么我假设您有一个文件my_app/__init__.py确保在该文件中,它导入任务my_app.tasks.my_task
使用my_app.tasks.other_task
以便 celery 应用程序知道此类任务存在。
# Contents of my_app/__init__.py
from my_app.tasks import (
my_task,
other_task,
)
In case the my_task
was just newly added (whereas other_task
was already an old existing task), you might not have restarted the celery worker yet to see the new task.如果
my_task
是新添加的(而other_task
已经是一个旧的现有任务),您可能还没有重新启动 celery worker 来看到新任务。 Try restarting the worker.尝试重新启动工作人员。
Another solution can be adding a name
param in the shared_task
decorator, ie,另一种解决方案是在
shared_task
装饰器中添加一个name
参数,即
@shared_task(queue='celery', name='other_task')
def other_task():
""" WORKS """
...
@shared_task(queue='celery', name='my_task')
def my_task():
""" DOES NOT WORK """
...
I see this in a full demo project https://github.com/melikesofta/django-dynamic-periodic-tasks , and the related code can be find here .我在一个完整的演示项目https://github.com/melikesofta/django-dynamic-periodic-tasks中看到了这一点,相关代码可以在这里找到。
I could solve the error by just restarting Celery .我可以通过重新启动Celery来解决错误。 I use Celery 5.1.2 :
我使用Celery 5.1.2 :
celery -A core worker --pool=solo -l info
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.