Django Celery收到类型为"appname.tasks.add"的未注册任务

Jac*_*ans 3 python django celery

按照文档和Demo Django项目https://github.com/celery/celery/tree/3.1/examples/django

项目结构

piesup2
    |
    piesup2
    |  |__init__.py
    |  |celery.py
    |  |settings.py
    |  |urls.py
    reports
       |tasks.py
       |models.py
       |etc....
Run Code Online (Sandbox Code Playgroud)

我的守则

piesup2/celery.py

from __future__ import absolute_import

import os

from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'piesup2.settings')

from django.conf import settings  # noqa

app = Celery('piesup2')

# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)


@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))
Run Code Online (Sandbox Code Playgroud)

piesup2/__init__.py

from __future__ import absolute_import

# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app  # noqa
Run Code Online (Sandbox Code Playgroud)

piesup2/reports/tasks.py

from __future__ import absolute_import

from celery import shared_task


@shared_task
def add(x, y):
    return x + y
Run Code Online (Sandbox Code Playgroud)

从命令行启动Celery后:

celery -A piesup2 worker -l info
Run Code Online (Sandbox Code Playgroud)

当我尝试从models.py文件中运行任务时,如下所示:

 def do_stuff(self):
    from reports.tasks import add
    number = add.delay(2, 2)
    print(number)
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

[2016-08-05 16:50:31,625:ERROR/MainProcess]收到类型为'reports.tasks.add'的未注册任务.该消息已被忽略并被丢弃.

你还记得导入包含这个任务的模块吗?或者你可能正在使用相对进口?有关更多信息,请参阅http://docs.celeryq.org/en/latest/userguide/tasks.html#task-names.

消息体的全部内容是:{'callbacks':None,'retries':0,'chord':None,'errbacks':None,'task':'reports.tasks.add','args': [2,2],'timelimit':[无,无],'kwargs':{},'id':'b12eb387-cf8c-483d-b53e-f9ce0ad6b421','taskset':无,'eta':无,'expires':无,'utc':True}(258b)Traceback(最近一次调用最后一次):文件"/home/jwe/piesup2/venv/lib/python3.4/site-packages/celery/worker/consumer .t",第456行,on_task_received策略[名称](消息,正文,KeyError:'reports.tasks.add'

luk*_*aus 14

在django设置中,您需要将具有芹菜任务的每个模块添加到CELERY_IMPORTS

CELERY_IMPORTS = (
    'reports.tasks',
    'some_app.some_module',
)
Run Code Online (Sandbox Code Playgroud)