[Python-ideas] Adding threading.RepeatTimer class

Christian Heimes lists at cheimes.de
Fri Jan 28 21:38:56 CET 2011


Am 27.01.2011 23:12, schrieb Brian Curtin:
> import threading
> 
> class RepeatTimer(threading.Thread):
>     def __init__(self, interval, callable, *args, **kwargs):
>         threading.Thread.__init__(self)
>         self.interval = interval
>         self.callable = callable
>         self.args = args
>         self.kwargs = kwargs
>         self.event = threading.Event()
>         self.event.set()
> 
>     def run(self):
>         while self.event.is_set():
>             t = threading.Timer(self.interval, self.callable,
>                                 self.args, self.kwargs)
>             t.start()
>             t.join()
> 
>     def cancel(self):
>         self.event.clear()

CherryPy has two simpler implementations for a PerpetualTimer and a
BackgroundTask:
http://cherrypy.org/browser/trunk/cherrypy/process/plugins.py#L424 . The
BackgroundTask is less CPU consuming under Python 2.x. I guess the
extended threading funtions in 3.2 make the implementation obsolete.

Christian




More information about the Python-ideas mailing list