我正在尝试在 Celery 中设置一个每 3 秒运行一次的虚拟任务,但到目前为止收效甚微。这是我得到的输出:
我已经按如下方式设置了 celery :
在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
我的设置方式有没有做错什么?
最佳答案
之所以回答是因为这是我在 CELERYBEAT_SCHEDULE
上搜索时的第一个结果。
它对我不起作用的原因是它应该是 CELERY_BEAT_SCHEDULE
关于python - CELERYBEAT_SCHEDULE 中的任务未被处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38774128/