CELERYBEAT_SCHEDULE 中的任务未处理

Tasks in CELERYBEAT_SCHEDULE not being processed

我正在尝试在 Celery 中设置一个每 3 秒运行一次的虚拟任务,但到目前为止收效甚微。这是我得到的输出:

我按如下方式设置了芹菜:

settings.py:

from datetime import timedelta

BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'UTC'
CELERY_IMPORTS = ("api.tasks")
CELERYBEAT_SCHEDULE = {
    'add_job': {
        'task': 'add_job',
        'schedule': timedelta(seconds=3),
        'args': (16, 16)
    },
}
CELERY_TIMEZONE = 'UTC'

celery.py:

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

# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'blogpodapi.settings')
app = Celery(
    'blogpodapi',
)

# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)


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

tasks.py

from celery.task import task

@task(name='add_job')
def add_job(x, y):
    r = x + y
    print "task arguments: {x}, {y}".format(x=x, y=y)
    print "task result: {r}".format(r=r)
    return r

我的设置方式有没有做错?

好的,我看到的最基本的错误是,您在 settings.py 中提到的大部分设置都需要进入 celery.py

特别是 CELERYBEAT_SCHEDULE

你做的一切都正确,只是你的 Celery 正在等待 一个任务,它从 celery.py 读取而不是从settings.py。因此什么也没有发生。

参考我的celery.pysettings.py

celery.py -> https://github.com/amyth/hammer/blob/master/config/celery.py

settings.py -> https://github.com/amyth/hammer/blob/master/config/settings.py

我使用了 crontab,因为我想在一天中的特定时间执行任务。所以你不必担心。你的非常适合你想做的事。

此外,无论您在关注 celery 的任何博客或教程,请再次检查究竟需要哪些设置,以及您是否需要所有这些设置。

之所以回答,是因为这是我在 CELERYBEAT_SCHEDULE 上搜索时的第一个结果。

它对我不起作用的原因是因为它应该是 CELERY_BEAT_SCHEDULE

关于为什么你的任务没有运行:没有注册。如果是这样,Celery worker 启动时的输出会有所不同——它将包含以下两行(至少):

[tasks]
  . add_job