我正在努力解决芹菜没有按照其文档所声称的那样做的问题:我有一个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/jobs/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外壳中:
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>}
你知道这里发生了什么吗?它只是在我自己导入模块之前不会加载任务。
提前谢谢。
也遇到了同样的问题,并将有问题的应用程序的settings.py中INSTALLED_APPS下的应用程序配置的点式python路径更改为仅包含应用程序的模块名称,并且似乎(目前)可以工作。在Django 1.9中,看起来INSTALLED_APPS可以采用应用程序配置文件的点式python路径,也可以像早期版本中那样只使用模块名称。