Some Celery tasks work, others - NotRegistered

I follow the tutorial on Django celery , and the tasks that I see in the example ( add, mul ) work fine for me. I get the correct answer when I do res = add.delay(1,2); res.get() res = add.delay(1,2); res.get() .

But I get *** NotRegistered: u'pipeline.tasks.sayhello' when I try to complete another of my tasks res = sayhello.delay('trex') .

If I do res = sayhello('trex') , then I can get the result simply by typing res . But in this way I execute the function ornidarly without using Celery.

The task only works if I run it in the Django ./manage shell

 >>> res = sayhello.delay('trex') >>> res.get() u'Hello trex' 

So the problem is that I cannot complete the sayhello task from pipeline/views.py . But I can do add and mul tasks from there.

Why? How to perform tasks from views.py ?

Error message:

 [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' 

version of django

 1.9.7 

Celery Version:

 celery==4.0.0 django-celery==3.1.17 

Django Project Tree:

 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 

ETL / pipeline / views.py

 from .tasks import * def get_response(request): result = add.delay(1, 2) result.get() result = sayhello.delay('tiger') result.get() 

ETL / pipeline / 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 

Also I tried this:

 from celery.decorators import task @task(name="sayhello") def sayhello(name): return "Hello {0}".format(name) 

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

ETL / __ init__py

 from __future__ import absolute_import, unicode_literals from .celery import app as celery_app __all__ = ['celery_app'] 

ETL / Settings / 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', ) 
+9
python django celery django-celery
source share
3 answers

The error is due to the fact that the CELERY_IMPORTS parameter does not work properly in the etl / settings / base.py file. Therefore my suggestion:

Remove the comma from

 CELERY_IMPORTS = ('pipeline.tasks' , ) 

And if the problem still persists, run the following command:

 celery -A pipeline.tasks worker --loglevel=DEBUG 

In addition, your tasks.py file must be in the Django application (registered in settings.py) to import it. Also check this item. Thanks.

+5
source share

Hope this helps someone. I changed my code and forgot to restart the celery worker.

Try restarting the celery workers .

0
source share

Relative imports and automatic generation of names do not combine with each other, so if you use relative imports, you must set the name explicitly.

For example, if a client imports the module "myapp.tasks" as ".tasks", and the employee imports the module as "myapp.tasks", the generated names will not match and the NotRegistered error will be NotRegistered employee.

http://docs.celeryproject.org/en/latest/userguide/tasks.html#task-naming-relative-imports

0
source share

All Articles