I have written a file mytasks.py
from celery import Celery
celery = Celery("tasks",
broker='redis://localhost:6379/0',
backend='redis')
@celery.task
def add(x,y):
return x+y
and task.py
as follow
from mytasks import add
add.delay(1,1)
I have started redis server and I have started celery server. but when i m running task.py then i am getting the following error:
Received unregistered task of type 'mytasks.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://bit.ly/gLye1c for more information.
The full contents of the message body was:
{'retries': 0, 'task': 'mytasks.add', 'eta': None, 'args': (1, 1), 'expires': None, 'callbacks': None, 'errbacks': None, 'kwargs': {}, 'id': 'a4792308-d575-4de4-8b67-26982cae2fa4', 'utc': True} (173b)
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/celery/worker/consumer.py", line 411, in on_task_received
strategies[name](message, body, message.ack_log_error)
KeyError: 'mytasks.add'
what may be the possibel reason
All tasks must be imported during Django and Celery startup so that Celery knows about them. If we put them in <appname>/tasks.py files and call app. autodiscover_tasks(), that will do it. Or we could put our tasks in our models files, or import them from there, or import them from application ready methods.
What is a Celery worker? Celery is a task management system that you can use to distribute tasks across different machines or threads. It allows you to have a task queue and can schedule and process tasks in real-time. This task queue is monitored by workers which constantly look for new work to perform.
Hey I have solved the problem i did one thing i add
CELERY_IMPORTS=("mytasks")
in my celeryconfig.py
file and i got succeed.
also can use include
param in Celery class: http://docs.celeryproject.org/en/latest/getting-started/next-steps.html#proj-celery-py
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With