一些Celery任务有效,另一些则是NotRegistered

srg*_*bnd 9 python django celery django-celery

我按照Celery Django教程,我在示例(add, mul)中看到的任务对我来说非常有用.我做的时候得到了正确的回答res = add.delay(1,2); res.get().

但是*** NotRegistered: u'pipeline.tasks.sayhello'当我尝试执行另一项任务时,我得到了res = sayhello.delay('trex').

如果我这样做,res = sayhello('trex')我只需输入即可得到结果res.但是通过这种方式,我在不使用Celery的情况下执行ornidarly函数.

该任务仅在我在Django shell中运行时才有效 ./manage shell

>>> res = sayhello.delay('trex')
>>> res.get()
u'Hello trex'
Run Code Online (Sandbox Code Playgroud)

所以,问题是我无法执行sayhello任务pipeline/views.py.但我可以执行任务add,并mul从那里.

这是为什么?如何正确运行任务views.py?

错误已完整消息:

[2016-11-11 10:56:09,870: ERROR/MainProcess] Received unregistered task of type u'pipeline.tasks.sayhello'.
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:
'[["tiger"], {}, {"chord": null, "callbacks": null, "errbacks": null, "chain": null}]' (84b)
Traceback (most recent call last):
  File "/home/trex/Development/Sirius/new/rocket/rocket-venv/local/lib/python2.7/site-packages/celery/worker/consumer/consumer.py", line 549, in on_task_received
    strategy = strategies[type_]
KeyError: u'pipeline.tasks.sayhello'
Run Code Online (Sandbox Code Playgroud)

Django版

1.9.7
Run Code Online (Sandbox Code Playgroud)

芹菜版:

celery==4.0.0
django-celery==3.1.17
Run Code Online (Sandbox Code Playgroud)

Django项目目录树:

rocket
??? etl
?   ??? etl
?   ?   ??? celery.py
?   ?   ??? __init__.py
?   ?   ??? settings
?   ?   ?   ??? base.py
?   ?   ?   ??? dev.py
?   ?   ?   ??? __init__.py
?   ?   ?   ??? production.py
?   ?   ?   ??? test.py
?   ?   ??? urls.py
?   ?   ??? wsgi.py
?   ??? manage.py
?   ??? pipeline
?   ?   ??? __init__.py
?   ?   ??? models.py
?   ?   ??? tasks.py
?   ?   ??? tests.py
?   ?   ??? urls.py
?   ?   ??? views.py
Run Code Online (Sandbox Code Playgroud)

ETL /管道/ views.py

from .tasks import *

def get_response(request):
    result = add.delay(1, 2)
    result.get()
    result = sayhello.delay('tiger')
    result.get()
Run Code Online (Sandbox Code Playgroud)

ETL /管道/ tasks.py

from __future__ import absolute_import, unicode_literals
from celery import shared_task

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

@shared_task
def mul(x, y):
    return x * y

@shared_task
def sayhello(name):
    return "Hello %s" % name
Run Code Online (Sandbox Code Playgroud)

我也试过这个:

from celery.decorators import task

@task(name="sayhello")
def sayhello(name):
    return "Hello {0}".format(name)
Run Code Online (Sandbox Code Playgroud)

ETL/celery.py

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'etl.settings.base')
app = Celery('etl')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

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

ETL/__ init__py

from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app
__all__ = ['celery_app']
Run Code Online (Sandbox Code Playgroud)

ETL /设置/ base.py

...
CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Europe/London'
CELERY_IMPORTS = ('pipeline.tasks', )
Run Code Online (Sandbox Code Playgroud)

Kar*_*nga 8

这可能有希望帮助某人。我修改了我的代码并忽略了重新启动芹菜工人。

Try restarting the celery workers.


Pra*_*edi 5

该错误是因为CELERY_IMPORTS设置无法正常进入您的etl/settings/base.py文件.所以我的建议是:

从中删除逗号

CELERY_IMPORTS = ('pipeline.tasks' , )
Run Code Online (Sandbox Code Playgroud)

如果问题仍然存在,请运行以下命令:

celery -A pipeline.tasks worker --loglevel=DEBUG
Run Code Online (Sandbox Code Playgroud)

还有一件事,你的tasks.py文件需要在一个Django应用程序(在settings.py中注册)才能导入.所以请检查这一点.谢谢.

  • 您在答案中提出的是删除逗号。这是错误的。如果没有逗号,则无法注册任务。看配置http://docs.celeryproject.org/en/v2.2.4/configuration.html的例子我试过了。没有逗号它真的不起作用。 (2认同)