Celery“收到未注册的任务类型”

Joe*_*aro 2 python celery django-celery celerybeat

我已经阅读了很多与此类似的帖子,但对我来说似乎没有任何意义。

我正在尝试将 Celery PeriodicTask 配置为每 5 秒触发一次,但我被 Celery 配置问题所困扰(我认为)

通讯/任务.py

import datetime
from celery.decorators import periodic_task

@periodic_task
def send_queued_messages():
    # do something...
Run Code Online (Sandbox Code Playgroud)

我的应用程序/settings.py

...
from comm.tasks import send_queued_messages
from datetime import timedelta
CELERYBEAT_SCHEDULE = {
    'send_queued_messages_every_5_seconds': {
        'task': 'comm.tasks.send_queued_messages',   # Is the issue here?  I've tried a dozen variations!!
        'schedule': timedelta(seconds=5),
        },
    }
Run Code Online (Sandbox Code Playgroud)

我的错误日志的相关输出:

23:41:00 worker.1 | [2015-06-10 03:41:00,657: ERROR/MainProcess] Received unregistered task of type 'send_queued_messages'.
23:41:00 worker.1 | The message has been ignored and discarded.
23:41:00 worker.1 | 
23:41:00 worker.1 | Did you remember to import the module containing this task?
23:41:00 worker.1 | Or maybe you are using relative imports?
23:41:00 worker.1 | Please see http://bit.ly/gLye1c for more information.
23:41:00 worker.1 | 
23:41:00 worker.1 | The full contents of the message body was:
23:41:00 worker.1 | {'utc': True, 'chord': None, 'args': [], 'retries': 0, 'expires': None, 'task': 'send_queued_messages', 'callbacks': None, 'errbacks': None, 'timelimit': (None, None), 'taskset': None, 'kwargs': {}, 'eta': None, 'id': 'a8ca18...227a56'} (216b)
Run Code Online (Sandbox Code Playgroud)

小智 9

我遇到了这个确切的问题,结果证明问题不在于任务的名称,而是 Celery 工作人员不知道您的任务模块。

换句话说,您有正确的任务名称 ( 'comm.tasks.send_queued_messages'),它是由任务装饰器生成的,只是您没有告诉 Celery 去哪里寻找它。

最快的解决方案是将以下内容添加到myapp/settings.py

CELERY_IMPORTS = ['comm.tasks']
Run Code Online (Sandbox Code Playgroud)

根据文档,这决定了“工作程序启动时要导入的模块顺序”。

或者,你可以配置你的设置自动发现任务(见文档在这里),但你必须命名空间的任务模块(S),移动comm/tasks.pycomm/comm/tasks.py

对我来说,混乱来自 Celery 的自动命名约定,它看起来像一个导入语句,这让我相信我是在CELERYBEAT_SCHEDULE['task']用来告诉 Celery在哪里寻找任务。相反,调度程序只是将名称作为字符串。