Django Celery Received unregistered task of type appname.tasks.add

2024/11/17 23:54:15

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.pyreports|tasks.py|models.py|etc....

My Code

piesup2/celery.py

from __future__ import absolute_importimport osfrom 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  # noqaapp = 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_importfrom 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 addnumber = add.delay(2, 2)print(number)

I get the following error:

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

Did you remember to import the module containing this task? Or maybeyou are using relative imports? Please see http://docs.celeryq.org/en/latest/userguide/tasks.html#task-names formore 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_receivedstrategies[name](message, body, KeyError: 'reports.tasks.add'

Answer

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',
)
https://en.xdnf.cn/q/71123.html

Related Q&A

Documenting and detailing a single script based on the comments inside

I am going to write a set of scripts, each independent from the others but with some similarities. The structure will most likely be the same for all the scripts and probably looks like: # -*- coding: …

Using Ansible variables in testinfra

Using TestInfra with Ansible backend for testing purposes. Everything goes fine except using Ansible itself while running teststest.pyimport pytest def test_zabbix_agent_package(host):package = host.pa…

How to create a dictionary of dictionaries of dictionaries in Python

So I am taking a natural language processing class and I need to create a trigram language model to generate random text that looks "realistic" to a certain degree based off of some sample da…

How to separate Master Slave (DB read / writes) in Flask Sqlalchemy

Im trying to separate the Read and write DB operations via Flask Sqlalchemy. Im using binds to connect to the mysql databases. I would want to perform the write operation in Master and Reads from slave…

Why import class from another file will call __init__ function?

The structure of the project is:project - main.py - session.py - spider.pyThere is a class in session.py:import requestsclass Session:def __init__(self):self.session = requests.Session()print(Session c…

Flask: login session times out too soon

While editing a record, if there is a long wait of let say a few minutes (getting coffee) and then coming back to press the save (POST), I get redirected to the main page to login instead and the data …

Activate virtual environement and start jupyter notebook all in batch file

I created the following batch file: jupyter_nn.bat. Inside file I have:cd "C:\My_favorite_path" activate neuralnets jupyter notebookSo the goal is to activate conda virtual environment and s…

several contour plots in the same figures

I have several 3d functions. I would like two plot the contour plots of them in the same figure to see the difference between them. I expect to see some crossings between contours of two functions. Her…

how to detect all the rectangular boxes in the given image

I tried to detect all the rectangles in image using threshold, canny edge and applied contour detection but it was not able to detect all the rectangles. Finally, I thought of detect the same using hou…

Python Pandas Series failure datetime

I think that this has to be a failure of pandas, having a pandas Series (v.18.1 and 19 too), if I assign a date to the Series, the first time it is added as int (error), the second time it is added as …