简体   繁体   中英

Django 1.9: CELERY_IMPORTS not working

I'm struggling with celery not doing what its documentation claims: I have a DJango 1.9 application and I'm running celery 3.1.20 and I have the following:

myapp/celery.py:

from __future__ import absolute_import
import os

from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.settings')
from django.conf import settings  # noqa

app = Celery('myapp')
app.config_from_object('django.conf:settings')

myapp/jobs/tasks.py:

from myapp.celery import app

class Job1(app.Task):
    ...
     name = 'job_1'
    ...


class Job2(app.Task):
    ...
     name = 'job_2'
    ...

However, I've tried both:

myapp/settings.py:

CELERY_IMPORTS = ('myapp.jobs.tasks',)

and

myapp/celery.py:

app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

and neither are registering my tasks properly. The tasks only show up in app.tasks once I manually import the module which defines the tasks, so I've had to do an ugly local-import hack when I use a task to ensure it's loaded.

In the django shell:

In [1]: from myapp.celery import app

In [2]: app.tasks
Out[2]:
{'celery.backend_cleanup': <@task: celery.backend_cleanup of myapp:0x10dc260d0>,
 'celery.chain': <@task: celery.chain of myapp:0x10dc260d0>,
 'celery.chord': <@task: celery.chord of myapp:0x10dc260d0>,
 'celery.chord_unlock': <@task: celery.chord_unlock of myapp:0x10dc260d0>,
 'celery.chunks': <@task: celery.chunks of myapp:0x10dc260d0>,
 'celery.group': <@task: celery.group of myapp:0x10dc260d0>,
 'celery.map': <@task: celery.map of myapp:0x10dc260d0>,
 'celery.starmap': <@task: celery.starmap of myapp:0x10dc260d0>}

In [3]: app.conf['CELERY_IMPORTS']
Out[3]: ('myapp.jobs.tasks',)

In [4]: from myapp.jobs import tasks

In [5]: app.tasks
Out[5]:
{'celery.backend_cleanup': <@task: celery.backend_cleanup of myapp:0x10dc260d0>,
 'celery.chain': <@task: celery.chain of myapp:0x10dc260d0>,
 'celery.chord': <@task: celery.chord of myapp:0x10dc260d0>,
 'celery.chord_unlock': <@task: celery.chord_unlock of myapp:0x10dc260d0>,
 'celery.chunks': <@task: celery.chunks of myapp:0x10dc260d0>,
 'celery.group': <@task: celery.group of myapp:0x10dc260d0>,
 'celery.map': <@task: celery.map of myapp:0x10dc260d0>,
 'celery.starmap': <@task: celery.starmap of myapp:0x10dc260d0>,
 'job_1': <@task: job_1 of myapp:0x10dc260d0>,
 'job_2': <@task: job_2 of myapp:0x10dc260d0>,
 'job_3': <@task: job_3 of myapp:0x10dc260d0>}

Any ideas what's going on here? It just doesn't load the tasks until I import the module myself.

Thanks in advance.

Had the same problem, and changed the dotted python path to the application config under INSTALLED_APPS in settings.py of the application in question, to just the name of the module with the application, and it seems to work (for now). In Django 1.9 it looks like INSTALLED_APPS can either take a dotted python path to the application config file, or just the module name like in earlier versions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM