最近,我开始使用带有Django 1.9.10的Celery 4.0.2来运行一些定期任务。基本上,即使我遵循官方文档中提到的步骤,我也会遇到两个问题:
当我手动将任务添加到Celery App并尝试运行worker时,我收到此错误:
RuntimeError:模型类core.models.Modl没有在INSTALLED_APPS
这就是我创建Celery App的方式:
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from django.conf import settings
from .scraper import scraper_example
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'conf')
app = Celery('celery_power',
broker=settings.CELERY_BROCKER_URL,
backend=settings.CELERY_RESULT_BACKEND,
include=['apps.core.tasks'])
# app.autodiscover_tasks() in case I don't include the tasks module,
# never worked by the way
对于我在核心应用程序目录中创建的tasks.py,我只需导入一些模型并使用它们插入一些数据,如下所示:
from .models import Modl
from celery import shared_task
@shared_task
def deal_with_modl():
m = Modl(name="model")
m.save()