Django定期任务芹菜

时间:2020-05-02 16:16:19

标签: python django celery

我是Django和Celery的新手。 请帮我,我不明白它是如何工作的。我想每1分钟在控制台中看到“ Hello world”。

tasks.py

from celery import Celery
from celery.schedules import crontab
from celery.task import periodic_task

app = Celery('tasks', broker='pyamqp://guest@localhost//')


@periodic_task(run_every=(crontab(hour="*", minute=1)), ignore_result=True)
def hello_world():
    return "Hello World"

celery.py

from __future__ import absolute_import

import os

from celery import Celery

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test.settings.local")

app = Celery('test')
app.config_from_object('celeryconfig')
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

# 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

celeryconfig.py

broker_url = 'redis://localhost:6379'
result_backend = 'rpc://'

task_serializer = 'json'
result_serializer = 'json'
accept_content = ['json']
timezone = 'Europe/Oslo'
enable_utc = True

这是一个简单的芹菜设置和代码,但是不起作用= \

celery -A tasks worker -B

什么也没有发生。告诉我我在做什么错?谢谢!

1 个答案:

答案 0 :(得分:1)

您需要在celeryconfig.py中设置beat_schedule

from celery.schedules import crontab
beat_schedule = {
    'send_each_minute': {
        'task': 'your.module.path.function',
        'schedule': crontab(),
        'args': (),
    },
}