我正在努力与芹菜不做它的文件声称:我有一个DJango 1.9应用程序,我正在运行芹菜3.1.20,我有以下内容:
的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 /作业/ tasks.py:
from myapp.celery import app
class Job1(app.Task):
...
name = 'job_1'
...
class Job2(app.Task):
...
name = 'job_2'
...
但是,我试过了两个:
的myapp / settings.py:
CELERY_IMPORTS = ('myapp.jobs.tasks',)
和
的myapp / celery.py:
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
并且都没有正确注册我的任务。一旦我手动导入定义任务的模块,任务就会显示在app.tasks中,所以当我使用任务来确保它被加载时,我不得不做一个丑陋的本地导入黑客。
在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>}
有什么想法在这里发生了什么?在我自己导入模块之前,它只是不加载任务。
提前致谢。
答案 0 :(得分:1)
有同样的问题,并将点线python路径更改为有问题的应用程序的settings.py中的INSTALLED_APPS下的应用程序配置,只更改了应用程序模块的名称,它似乎工作(现在) 。在Django 1.9中,看起来INSTALLED_APPS可以采用点缀的python路径到应用程序配置文件,或者只是像早期版本中那样使用模块名称。