Python/Jython - threading.Timer()

Ype Kingma ykingma at accessforall.nl
Thu Dec 19 14:12:06 EST 2002


VLP wrote:

> Timer class of threading module is not present in
> Jython. Anyone know where I can find a replacement? Or
> is there a portable way to get same functionality
> across Python and Jython?
> 
> Regards,
> VLP

The Timer class is new in 2.2.
This might do the trick:

from threading import Thread
import time

class Timer(Thread):
    def __init__(self, interval, function, *args, **kwargs):
        Thread.__init__()
        self.interval = interval
        self.function = function
        self.args = args
        self.kwargs = kwargs
        self.start()

    def run(self):
        time.sleep(self.interval)
        return self.function(*self.args, **self.kwargs)

I'm not sure about the args and kwargs stuff, but it works without 
arguments.
You could probably just copy the Timer class from the python 2.2
distribution; I don't have it ready here.

Have fun,
Ype




More information about the Python-list mailing list