Celery: auto discovery does not find tasks module in app

Martin picture Martin · Apr 19, 2012 · Viewed 24.7k times · Source

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?

Answer

dgel picture dgel · Apr 19, 2012

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