特定时间的Django Celery定期任务 [英] Django Celery Periodic Task at specific time

查看:68
本文介绍了特定时间的Django Celery定期任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using celery==4.1.1 in my project. In my settings.py, I have the following:

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分钟)运行>.我尝试了每天在给定时间运行函数的多种方法,但未能实现相同的功能.

The second task mentioned in CELERY_BEAT_SCHEDULE is running perfectly. However, the first task mathematica.core.tasks.another_test which is a simple function returning a string is not running at the specified time, 00:45 (45 minutes past midnight). I have tried a number of ways to run a function at a given time each day but failed to achieve the same.

请提出实现相同结果的方法/提示.

Please suggest ways/hints to achieve the same results.

推荐答案

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


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

上面的代码对我有用.

这篇关于特定时间的Django Celery定期任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆