如何在 Python 中获得类似 Cron 的调度程序? [英] How do I get a Cron like scheduler in Python?

查看:32
本文介绍了如何在 Python 中获得类似 Cron 的调度程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个 Python 库,它将提供 atcron 之类的功能.

I'm looking for a library in Python which will provide at and cron like functionality.

我非常想要一个纯 Python 解决方案,而不是依赖安装在盒子上的工具;这样我就可以在没有 cron 的机器上运行了.

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.

对于那些不熟悉 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.

cron 时间表达式语法不那么重要,但我想要一些具有这种灵活性的东西.

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.

编辑我对启动进程不感兴趣,只是用 Python 编写的作业"——python 函数.根据需要,我认为这将是一个不同的线程,但不会在不同的过程中.

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.

为此,我正在寻找 cron 时间表达式的表现力,但在 Python 中.

To this end, I'm looking for the expressivity of the cron time expression, but in Python.

Cron 已经存在多年,但我正在努力尽可能地便携.我不能依赖它的存在.

Cron has been around for years, but I'm trying to be as portable as possible. I cannot rely on its presence.

推荐答案

如果您正在寻找轻量级结帐schedule:

If you're looking for something lightweight checkout schedule:

import schedule
import time

def job():
    print("I'm working...")

schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)

while 1:
    schedule.run_pending()
    time.sleep(1)

披露:我是那个图书馆的作者.

Disclosure: I'm the author of that library.

这篇关于如何在 Python 中获得类似 Cron 的调度程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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