Python scheduler

Frank Millman frank at chagford.com
Thu Feb 21 06:42:30 CET 2013


On 21/02/2013 06:04, Rita wrote:
> Hello,
>
>
> Here is what I am trying to do. (Currently, I am doing this in cron but
> i need much more granularity). I am trying to run program every 20 secs
> and loop forever. I have several of these types of processes, some
> should run every 5 mins, 10 secs, 20 secs, 1 min and so forth. I was
> wondering what is the best way to do this?
>
>
> Also, would Greenlet do something I am asking for?
>

Don't know if this helps, but ...

I have a program with a main loop that listens for input, and 
simultaneously I need to run something in the background on a regular 
basis. I use the threading module, like this.

import threading

class Check(threading.Thread):
     def __init__(self):
         threading.Thread.__init__(self)
         self.event = threading.Event()

     def run(self):
         event = self.event  # make local
         while not event.is_set():
             [run your activity here]
             event.wait(10)  # check every 10 seconds

     def stop(self):
         self.event.set()

Before the main loop starts, I have -

     check = Check()
     check.start()

When the main loop ends, I have -

     check.stop()
     check.join()

For multiple activities, you could have one class per activity, or have 
a generic class, and pass in the executable and the timeout as parameters.

HTH

Frank Millman





More information about the Python-list mailing list