如何在 Python 中获得类似 Cron 的调度程序?
我正在寻找一个Python 库,它将提供at
和cron
之类的功能。
我很想有一个纯Python的解决方案,而不是依赖于盒子上安装的工具; 这样我就可以在没有 cron 的机器上运行。
对于那些不熟悉 cron 的人:您可以根据如下表达式来安排任务:
0 2 * * 7 /usr/bin/run-backup # run the backups at 0200 on Every Sunday
0 9-17/2 * * 1-5 /usr/bin/purge-temps # run the purge temps command, every 2 hours between 9am and 5pm on Mondays to Fridays.
cron 时间表达式语法不太重要,但我希望拥有这种灵活性的东西。
如果没有什么东西可以为我提供开箱即用的功能,那么任何有关构建块的建议都将非常感激。
编辑 我对启动进程不感兴趣,只是也用 Python 编写的“作业” - python 函数。 我认为这必然是一个不同的线程,但不是在不同的进程中。
为此,我正在寻找 cron 时间表达式的表达能力,但是是在 Python 中。
Cron 已经存在很多年了,但我正在努力使其尽可能便携。 我不能依赖它的存在。
I'm looking for a library in Python which will provide at
and cron
like functionality.
I'd quite like have a pure Python solution, rather than relying on tools installed on the box; this way I run on machines with no cron.
For those unfamiliar with cron
: you can schedule tasks based upon an expression like:
0 2 * * 7 /usr/bin/run-backup # run the backups at 0200 on Every Sunday
0 9-17/2 * * 1-5 /usr/bin/purge-temps # run the purge temps command, every 2 hours between 9am and 5pm on Mondays to Fridays.
The cron time expression syntax is less important, but I would like to have something with this sort of flexibility.
If there isn't something that does this for me out-the-box, any suggestions for the building blocks to make something like this would be gratefully received.
Edit
I'm not interested in launching processes, just "jobs" also written in Python - python functions. By necessity I think this would be a different thread, but not in a different process.
To this end, I'm looking for the expressivity of the cron time expression, but in Python.
Cron has been around for years, but I'm trying to be as portable as possible. I cannot rely on its presence.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您正在寻找轻量级结账时间表:
披露:我是该库的作者。
If you're looking for something lightweight checkout schedule:
Disclosure: I'm the author of that library.
您可以只使用普通的 Python 参数传递语法来指定您的 crontab。 例如,假设我们定义一个 Event 类如下:(
注意:未经过彻底测试)
然后您的 CronTab 可以用普通的 Python 语法指定为:
这样您就可以获得 Python 参数机制的全部功能(混合位置参数和关键字参数,以及可以使用符号名称表示周和月的名称)
CronTab 类将被定义为简单地以分钟为增量休眠,并在每个事件上调用 check() 。 (不过,夏令时/时区可能有一些微妙之处需要警惕)。 这是一个快速实现:
需要注意的一些事情:Python 的工作日/月份是零索引的(与 cron 不同),并且该范围不包括最后一个元素,因此像“1-5”这样的语法变成范围(0,5) - 即 [0 ,1,2,3,4]。 如果您更喜欢 cron 语法,那么解析它应该不会太困难。
You could just use normal Python argument passing syntax to specify your crontab. For example, suppose we define an Event class as below:
(Note: Not thoroughly tested)
Then your CronTab can be specified in normal python syntax as:
This way you get the full power of Python's argument mechanics (mixing positional and keyword args, and can use symbolic names for names of weeks and months)
The CronTab class would be defined as simply sleeping in minute increments, and calling check() on each event. (There are probably some subtleties with daylight savings time / timezones to be wary of though). Here's a quick implementation:
A few things to note: Python's weekdays / months are zero indexed (unlike cron), and that range excludes the last element, hence syntax like "1-5" becomes range(0,5) - ie [0,1,2,3,4]. If you prefer cron syntax, parsing it shouldn't be too difficult however.
或多或少与上面相同,但使用 gevent 并发:)
More or less same as above but concurrent using gevent :)
Croniter:解析 CRON 字符串并获取下一个事件时间的序列
没有列出的解决方案采用解析 CRON 计划字符串的方法。 所以,这是我的版本,使用 croniter。 基本要点:
简单!
具有更详细逻辑的更复杂用例:
辅助例程:
Croniter: Parse a CRON string and get sequence of next event times
None of the listed solutions take the approach of parsing a CRON schedule string. So, here is my version, using croniter. Basic gist:
Easy!
More complex usecases with more detailed logic:
Helper routines:
我喜欢 pycron 包解决这个问题的方式。
I like how the pycron package solves this problem.
我知道有很多答案,但另一个解决方案可能是使用装饰器。 这是每天在特定时间重复某个功能的示例。 使用这种方式的一个很酷的想法是,您只需将语法糖添加到您想要调度的函数中:
装饰器将如下所示:
I know there are a lot of answers, but another solution could be to go with decorators. This is an example to repeat a function everyday at a specific time. The cool think about using this way is that you only need to add the Syntactic Sugar to the function you want to schedule:
And the decorator will look like:
没有“纯 python”方法可以做到这一点,因为其他一些进程必须启动 python 才能运行您的解决方案。 每个平台都会有一种或二十种不同的方式来启动流程并监控其进度。 在 Unix 平台上,cron 是旧标准。 在 Mac OS X 上还有 launchd,它结合了类似 cron 的启动和看门狗功能,如果您愿意的话,可以让您的进程保持活动状态。 python 运行后,您可以使用 sched 模块 来调度任务。
There isn't a "pure python" way to do this because some other process would have to launch python in order to run your solution. Every platform will have one or twenty different ways to launch processes and monitor their progress. On unix platforms, cron is the old standard. On Mac OS X there is also launchd, which combines cron-like launching with watchdog functionality that can keep your process alive if that's what you want. Once python is running, then you can use the sched module to schedule tasks.
另一个简单的解决方案是:
aqcron.At 类是:
Another trivial solution would be:
And the class aqcron.At is:
我不知道类似的东西是否已经存在。 使用时间、日期时间和/或日历模块编写自己的模块很容易,请参阅 http:// /docs.python.org/library/time.html
python 解决方案的唯一问题是您的作业需要始终运行,并且可能在重新启动后自动“复活”,这是您 确实需要依赖系统相关的解决方案。
I don't know if something like that already exists. It would be easy to write your own with time, datetime and/or calendar modules, see http://docs.python.org/library/time.html
The only concern for a python solution is that your job needs to be always running and possibly be automatically "resurrected" after a reboot, something for which you do need to rely on system dependent solutions.