Django Celery收到未注册的任务,类型为'appname.tasks.add' [英] Django Celery Received unregistered task of type 'appname.tasks.add'

查看:2969
本文介绍了Django Celery收到未注册的任务,类型为'appname.tasks.add'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在文档和Demo Django项目之后, https://github.com /celery/celery/tree/3.1/examples/django



项目结构

  piesup2 
|
piesup2
| | __init __。py
| | celery.py
| | settings.py
| | urls.py
报告
| tasks.py
| models.py
| etc ....

我的代码



piesup2 / celery.py

 <_ c $ c> from __future__ import absolute_import 

import os

从芹菜进口芹菜

#设置'芹菜'程序的默认Django设置模块。
os.environ.setdefault('DJANGO_SETTINGS_MODULE','piesup2.settings')

from django.conf import settings#noqa

app = Celery('piesup2 ')

#在这里使用一个字符串表示工作人员在使用Windows时不必使用
#pickle该对象。
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))

piesup2 / __ init __。py

 code>从__future__导入absolute_import 

#这将确保在
#Django启动时始终导入应用程序,以便shared_task将使用此应用程序。
from .celery import app as celery_app#noqa

piesup2 /报告/ tasks.py

 从__future__导入absolute_import 

从芹菜import shared_task


@shared_task
def add(x,y):
return x + y

当我尝试从我的models.py文件中运行任务,如这个:

  def do_stuff(self):
from reports.tasks import add
number = add。延迟(2,2)
打印(数量)

我收到以下错误: / p>


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



您是否记得导入包含此任务的模块?或者也许
你正在使用相对的进口?请参阅 http://docs.celeryq.org/en/latest /userguide/tasks.html#task-names
更多信息。



消息体的完整内容是:{'callbacks' :
'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)追踪
(最近的最后一次调用):文件
/ home / jwe / piesup2 / venv / lib / python3。 4 / site-packages / celery / worker / consumer.py,
line 456,on_task_received
strategies [name](message,body,KeyError:'reports.tasks.add'



解决方案

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

  CELERY_IMPORTS =(
'reports.tasks',
'some_app.some_module ',


Following the documentation and the Demo Django project here https://github.com/celery/celery/tree/3.1/examples/django

Project Structure

piesup2
    |
    piesup2
    |  |__init__.py
    |  |celery.py
    |  |settings.py
    |  |urls.py
    reports
       |tasks.py
       |models.py
       |etc....

My Code

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

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

piesup2/reports/tasks.py

from __future__ import absolute_import

from celery import shared_task


@shared_task
def add(x, y):
    return x + y

After starting Celery from the command line with:

celery -A piesup2 worker -l info

When I attempt to run a task from within my models.py file like this:

 def do_stuff(self):
    from reports.tasks import add
    number = add.delay(2, 2)
    print(number)

I get the following error:

[2016-08-05 16:50:31,625: ERROR/MainProcess] Received unregistered task of type 'reports.tasks.add'. The message has been ignored and discarded.

Did you remember to import the module containing this task? Or maybe you are using relative imports? Please see http://docs.celeryq.org/en/latest/userguide/tasks.html#task-names for more information.

The full contents of the message body was: {'callbacks': None, 'retries': 0, 'chord': None, 'errbacks': None, 'task': 'reports.tasks.add', 'args': [2, 2], 'timelimit': [None, None], 'kwargs': {}, 'id': 'b12eb387-cf8c-483d-b53e-f9ce0ad6b421', 'taskset': None, 'eta': None, 'expires': None, 'utc': True} (258b) Traceback (most recent call last): File "/home/jwe/piesup2/venv/lib/python3.4/site-packages/celery/worker/consumer.py", line 456, in on_task_received strategies[name](message, body, KeyError: 'reports.tasks.add'

解决方案

In your django settings you need to add each module that has a celery task to CELERY_IMPORTS

CELERY_IMPORTS = (
    'reports.tasks',
    'some_app.some_module',
)

这篇关于Django Celery收到未注册的任务,类型为'appname.tasks.add'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆