简体   繁体   English

注册任务时,KeyError 在 celery 上收到类型为“”的未注册任务

[英]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:您可能想查看一些可能的解决方案:

Possible Solution 1:可能的解决方案 1:

You probably haven't configured correctly either:您可能还没有正确配置:

  • Celery imports eg celery_app.conf.update(imports=['my_app.tasks']) or celery_app.conf.imports = ['my_app.tasks'] Celery 导入例如celery_app.conf.update(imports=['my_app.tasks'])celery_app.conf.imports = ['my_app.tasks']
  • Or Celery include ( example ) eg celery_app = Celery(..., include=['my_app.tasks'])或 Celery 包括示例)例如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中自动发现

Possible Solution 2:可能的解决方案 2:

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,
)

Possible Solution 3:可能的解决方案 3:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM