特定时间的Django芹菜周期性任务

时间:2018-06-01 19:26:21

标签: python django celery

我在我的项目中使用celery==4.1.1。在我的settings.py中,我有以下内容:

from celery.schedules import crontab

CELERY_BROKER_URL = "redis://127.0.0.1:6379/1"
CELERY_TIMEZONE = 'Asia/Kolkata'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_RESULT_BACKEND = "redis://127.0.0.1:6379/1"


CELERY_BEAT_SCHEDULE = {
    'task-number-one': {
        'task': 'mathematica.core.tasks.another_test',
        'schedule': crontab(minute=45, hour=00)
    },
    'task-number-two': {
        'task': 'mathematica.core.tasks.test',
        'schedule': crontab(hour='*/1')
    }
}

CELERY_BEAT_SCHEDULE中提到的第二项任务运行正常。但是,返回字符串的简单函数的第一个任务mathematica.core.tasks.another_test未在指定时间00:45 (45 minutes past midnight)运行。我已经尝试了多种方法在每天的给定时间运行一个函数,但未能达到相同的目的。

请提供方法/提示以获得相同的结果。

1 个答案:

答案 0 :(得分:1)

'automatic_daily_report': {
            'task': 'tasks.daily_reports',
            'schedule': crontab(hour=0, minute=0),
            'args': None
        }


@shared_task()
def daily_reports():
    print("Mid- Night")

上面的代码对我有用。