I have the following setup with a fresh installed celery and django 1.4:
settings.py:
import djcelery
djcelery.setup_loader()
BROKER_HOST = 'localhost'
BROKER_PORT = 5672
BROKER_USER = 'user'
BROKER_PASSWORD = 'password'
BROKER_VHOST = 'test'
[...]
INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.admin',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.staticfiles',
'djcelery',
'south',
'compressor',
'testapp',
]
testapp/tasks.py:
from celery.task import task
@task()
def add(x, y):
return x + y
Message delivery to the celeryd works fine, but the task is always unregistered (so auto discovery does not seem to work correctly here). Only if I import the tasks module in tasks/__init__.py
the task is found and I can use it.
Also the documentation was a little confusing about the decorator import, but I think this is the right one now.
Where is the bug in my setup?
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.
This way, you delegate queue creation to Celery. You can use apply_async with any queue and Celery will handle it, provided your task is aware of the queue used by apply_async . If none is provided then the worker will listen only for the default queue.
Celery is a task queue implementation for Python web applications used to asynchronously execute work outside the HTTP request-response cycle. Celery is an implementation of the task queue concept. Learn more in the web development chapter or view the table of contents for all topics.
Add CELERY_IMPORTS
to your settings.py:
CELERY_IMPORTS = ('testapp.tasks',)
Import all the tasks in testapp.tasks.__init__
file
Then Celery will import all tasks from testapp.tasks folder and name them as they are
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