特定时间的Django Celery定期任务

编程入门 行业动态 更新时间:2024-10-09 17:30:02
本文介绍了特定时间的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定期任务

本文发布于:2023-11-27 14:24:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638443.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   Django   Celery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!