无法在 Django 项目中启动 Celery worker
Can't launch Celery worker within a Django project
最近,我开始使用 Celery 4.0.2 和 Django 1.9.10 来 运行 一些周期性任务。基本上,即使我按照官方文档中提到的步骤进行操作,我 运行 还是遇到了两个问题:
- Celery 应用未发现包含在每个应用目录中的任务
当我手动将任务添加到 Celery 应用程序并尝试 运行 工作人员时,我收到此错误:
RuntimeError: 模型 class core.models.Modl 没有显式声明 app_label 并且不在 INSTALLED_APPS
[=25 中的应用程序中=]
这就是我创建 Celery 应用程序的方式:
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()
原来这个问题与我构建 Django 项目的方式有关。本质上,当 django 启动时,芹菜应用程序没有加载。通过删除 celery_power 目录(我只是使用位于项目根级别的单个文件 celery_app.py )并遵循下面的结构,celery 应用程序能够自动发现我的所有任务并且我能够毫无问题地启动我的工作人员:
最近,我开始使用 Celery 4.0.2 和 Django 1.9.10 来 运行 一些周期性任务。基本上,即使我按照官方文档中提到的步骤进行操作,我 运行 还是遇到了两个问题:
- Celery 应用未发现包含在每个应用目录中的任务
当我手动将任务添加到 Celery 应用程序并尝试 运行 工作人员时,我收到此错误:
RuntimeError: 模型 class core.models.Modl 没有显式声明 app_label 并且不在 INSTALLED_APPS
[=25 中的应用程序中=]
这就是我创建 Celery 应用程序的方式:
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()
原来这个问题与我构建 Django 项目的方式有关。本质上,当 django 启动时,芹菜应用程序没有加载。通过删除 celery_power 目录(我只是使用位于项目根级别的单个文件 celery_app.py )并遵循下面的结构,celery 应用程序能够自动发现我的所有任务并且我能够毫无问题地启动我的工作人员: