用扭曲的延迟和循环调用模拟crontab

use*_*446 0 python cron twisted scheduler

我想用扭曲的应用程序实现类似cron的行为.我想触发定期通话(假设每周一次),但仅在准确的时间运行,而不是在我启动应用程序时运行.

我的用例如下:我的python应用程序在本周的任何时间启动.我希望每个星期一早上8点开始通话.但是我不想让活动等待(使用time.sleep()),我想使用callLater在下周一触发调用,然后从该日期开始循环调用.

任何想法/建议?谢谢,J.

fmo*_*moo 5

如果你完全爱上了cron风格的说明符,你也可以考虑使用parse-crontab

然后你的代码基本上看起来像:

from crontab import CronTab
monday_morning = CronTab("0 8 * * 1")

def do_something():
    reactor.callLater(monday_morning.next(), do_something)
    # do whatever you want!

reactor.callLater(monday_morning.next(), do_something)
reactor.run()
Run Code Online (Sandbox Code Playgroud)