python - celery "Received unregistered task of type"

标签 python celery django-celery celerybeat

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

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

comm/tasks.py

import datetime
from celery.decorators import periodic_task

@periodic_task
def send_queued_messages():
    # do something...

myapp/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),
        },
    }

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

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)

最佳答案

我遇到了这个确切的问题,事实证明问题不在于任务的名称,而是 Celery worker 不知道您的任务模块。

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

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

CELERY_IMPORTS = ['comm.tasks']

根据 the docs ,这决定了“工作程序启动时要导入的模块序列。”

或者,您可以将设置配置为自动发现任务 (see docs here),但是您必须为任务模块命名空间,将 comm/tasks.py 移动到 comm/comm/tasks.py.

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

关于python - celery "Received unregistered task of type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30746807/

相关文章:

python - 当 Javascript 刷新时,Selenium WebDriver waitForElementPresent

php - 在 linux apache 上使 php 运行 python 脚本的权限

python-3.x - celery 是长时间运行任务的好选择吗?

python - Django celery - asyncio - 守护进程不允许有 child

python - celery 不与 flask 应用程序一起运行

twitter - 与 Twitter 通信的 Celery Task

python - ProcessPoolExecutor 日志记录无法在 Windows 上登录内部功能,但在 Unix/Mac 上则不能

python - 在 Go 中创建哈希

python - 使用 celery 输出到文件

python - 无法运行本地 Celery Worker,没有名为 myapp 的模块