芹菜-安排在特定时间开始的定期任务 [英] Celery - schedule periodic tasks starting at a specific time

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

问题描述

安排从特定日期时间开始的定期任务的最佳方法是什么?

What is the best way to schedule a periodic task starting at specific datetime?

(考虑到我需要安排时间,因此我不使用cron大约一百个远程rsync,
用于计算远程vs本地偏移,并且需要在每个主机中生成日志的 second rsync每个路径。)

(I'm not using cron for this considering I've the need to schedule about a hundred remote rsyncs, where I compute the remote vs local offset and would need to rsync each path the second the logs are generated in each host.)

据我所知,celery.task.schedules crontab类仅允许指定小时,分钟,星期几。
到目前为止,我发现的最有用的提示是 nosklo的答案

By my understanding the celery.task.schedules crontab class only allows specifying hour, minute, day of week. The most useful tip I've found so far was this answer by nosklo.

这是最好的解决方案吗?
我使用了错误的工具吗?

Is this the best solution? Am I using the wrong tool for the job?

推荐答案

对于您的日程安排问题,Celery似乎是一个不错的解决方案: Celery的PeriodicTasks的运行时间解析度以秒为单位。

Celery seems like a good solution for your scheduling problem: Celery's PeriodicTasks have run time resolution in seconds.

您在此处使用适当的工具,但crontab条目不是您想要的。您想使用python的datetime.timedelta对象;

You're using an appropriate tool here, but the crontab entry is not what you want. You want to use python's datetime.timedelta object; the crontab scheduler in celery.schedules has only minute resolution, but using timedelta's to configure the PeriodicTask interval provides strictly more functionality, in this case, per second resolution.

例如,使用celery.schedules的crontab计划程序只有几分钟的分辨率,但是使用timedelta来配置PeriodicTask间隔则提供了更多的功能(在这种情况下,每秒分辨率)。

e.g. from the Celery docs

>>> from celery.task import tasks, PeriodicTask
>>> from datetime import timedelta
>>> class EveryThirtySecondsTask(PeriodicTask):
...     run_every = timedelta(seconds=30)
...
...     def run(self, **kwargs):
...         logger = self.get_logger(**kwargs)
...         logger.info("Execute every 30 seconds")

http:// ask .github.com / celery / reference / celery.task.base.html#celery.task.base.PeriodicTask

class datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)

这里唯一的挑战是您必须描述任务执行的频率,而不是任务执行的时间。但是,我建议您查看Advanced Python Scheduler http://packages.python.org/APScheduler/

The only challenge here is that you have to describe the frequency with which you want this task to run rather than at what clock time you want it to run; however, I would suggest you check out the Advanced Python Scheduler http://packages.python.org/APScheduler/

看起来,可以使用Advanced Python Scheduler自己的计划功能轻松地在您选择的任何计划下启动正常(即非周期性)Celery任务。

It looks like Advanced Python Scheduler could easily be used to launch normal (i.e. non Periodic) Celery tasks at any schedule of your choosing using it's own scheduling functionality.

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

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