django.core.exceptions.AppRegistryNotReady:尚未加载应用。用芹菜打败

时间:2018-05-04 09:19:05

标签: django celery supervisord

我有一个定期任务,每秒创建和保存模型。所以我配置了这个

CELERY_IMPORTS = (
    'myapp.tasks',
)

CELERY_BEAT_SCHEDULE = {
    'periodic_task': {
        'task': 'myapp.tasks.periodic_task',
        'schedule': timedelta(seconds=1),
    },
}

和典型的 celery.py

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from django.conf import settings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.settings')

app = Celery('myapp')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()

@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

最后我配置了 supervisord 来启动它:

[program:myapp-celery]
command=/home/me/myapp/venv/bin/celery worker -A myapp --loglevel=INFO
directory=/home/me/myapp
user=nobody
numprocs=1
stdout_logfile=/home/me/myapp/logs/celery.log
stderr_logfile=/home/me/myapp/logs/celery.log
autostart=true
autorestart=true
startsecs=10

stopwaitsecs = 600
stopasgroup=true
priority=1000

它在 tasks.py 中启动,它使用导入的模型,据我所知还没有加载,导致此错误

django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.>

所以我想要一些钩子或标志,表明一切都已设定,以便我的日程安排可以定期开始执行。

2 个答案:

答案 0 :(得分:2)

不确定是否与芹菜4.2.0rc3相关或不相关,但此处有报道:

https://github.com/celery/celery/issues/4699

并修复此处:

https://github.com/celery/celery/pull/4719

答案 1 :(得分:0)

你可以尝试这个命令:

celery -A myapp worker -l info