【发布时间】:2018-08-03 13:02:04
【问题描述】:
我正在尝试将任务设置为每十秒运行一次。使用 Celery Beat。
我正在使用:
Django==1.11.3
celery==4.1.0
django-celery-beat==1.1.1
django-celery-results==1.0.1
它给了我以下错误:
收到“operations.tasks.message”类型的未注册任务
我是 Celery 的新手,我尝试了很多解决方案,但似乎找不到解决方案,不胜感激
settings.py
CELERY_BROKER_URL = 'pyamqp://guest@localhost//'
CELERY_RESULT_BACKEND = 'django-db'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Africa/Johannesburg'
CELERY_BEAT_SCHEDULE = {
'message': {
'task': 'operations.tasks.message',
'schedule': 10.0
}
}
celery.py
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'nodiso.settings')
app = Celery('nodiso')
# 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.request))
__init__.py
from __future__ import absolute_import, unicode_literals
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app
__all__ = ['celery_app']
task.py
from __future__ import absolute_import, unicode_literals
from celery import shared_task
from operations import models
from .celery import periodic_task
@task
def message():
t = models.Celerytest.objects.create(Message='Hello World')
t.save()
文件结构
proj-
proj-
__init__.py
settings.py-
celery.py-
app-
tasks.py-
【问题讨论】:
-
IIRC Celery taks 名称(默认情况下)是方法名称,因此您的任务可能仅使用名称“message”注册,而不是配置中的“operations.tasks.message”。
-
您的文件名为
task.py(至少在您的问题描述中),但您提供的是此路径而不是operations.tasks.message,请注意S。另外,您正在使用装饰器@task,但它没有定义(导入)
标签: python django celery celerybeat