Search code examples
pythoncrontwistedscheduler

simulate crontab with twisted deferred and looping calls


I would like to implement a cron-like behaviour with my twisted application. I want to trigger a periodic call (let's say every week) but running at a precise time only, not when i start my application.

My use case is the following: my python application is started at any time in the week. I want the calls to be performed every monday at 8am. But I don't want to perorm active waiting (using a time.sleep()), i would like to use callLater to trigger the call next monday and then start a looping call from that date.

any idea/advice? thanks, J.


Solution

  • If you are absolutely in love with cron-style specifiers, you could also consider using parse-crontab

    Then your code looks basically like:

    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()