我在我的项目中有这样一个布局:(正如文档所说的那样)
/zonia
/backend
__init__.py
celery.py
...
/musics
tasks.py
...
...
在init.py中:
^{pr2}$在芹菜公司名称:
from __future__ import absolute_import, unicode_literals
import os
import environ
from celery import Celery
env = environ.Env()
environ.Env.read_env()
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('C_FORCE_ROOT', 'true')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings')
app = Celery('backend', backend='rpc://', broker=env('broker'))
# Using a string here means the worker don't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self))
我在任务.py模块如下:
@shared_task
def recalc_ranking_music():
musics = musics_models.Music.objects.all().order_by("-num_points")
for music, rank in enumerate(musics):
music.ranking = rank + 1
music.save()
当我用命令启动celeri时:celeri-A后端worker-l info
如你所见,我在任务.py模块不会被芹菜进程读取,但是芹菜.py文件夹可以。在
奇怪的是,我在另一个项目中有相同的布局,而且效果很好。在
我有两天的时间,这真的需要一些时间,有什么帮助吗?在
UPDATE:(from comment)“music”是一个django应用程序,所以它有一个__init__.py
文件。在
我还尝试过将应用程序名称传递给celery实例上的auto discover方法,但没有成功。在
如果我把app.autodiscover_任务(force=True)它抛出一个错误:
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
我找到了一个解决方案,我只需导入
celery.py
文件中的模块(Django App),它读取所有任务。在但是,这不是芹菜文档中描述的行为
您的问题可能与运行Celery的范围有关(例如:虚拟环境)
例如,当我像这样经营芹菜时:
它只输出一个任务(我的演示应用程序中唯一的任务):
^{pr2}$但是当我从virtualenv运行它时,结果是:
看到了吗?它发现了一个新的应用程序仅仅是因为环境。在
您的
musics
目录是否有__init__.py
?在另外,请尝试显式指定包名称:
app.autodiscover_tasks(['musics'])
芹菜文档here
相关问题 更多 >
编程相关推荐