A more simple resettable timer class, by adding few logic from python builtin Timer code. the print statement in each function call is for debugging purposes only, to show if it works.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | from threading import Thread, Event, Timer
import time
def TimerReset(*args, **kwargs):
""" Global function for Timer """
return _TimerReset(*args, **kwargs)
class _TimerReset(Thread):
"""Call a function after a specified number of seconds:
t = TimerReset(30.0, f, args=[], kwargs={})
t.start()
t.cancel() # stop the timer's action if it's still waiting
"""
def __init__(self, interval, function, args=[], kwargs={}):
Thread.__init__(self)
self.interval = interval
self.function = function
self.args = args
self.kwargs = kwargs
self.finished = Event()
self.resetted = True
def cancel(self):
"""Stop the timer if it hasn't finished yet"""
self.finished.set()
def run(self):
print "Time: %s - timer running..." % time.asctime()
while self.resetted:
print "Time: %s - timer waiting for timeout in %.2f..." % (time.asctime(), self.interval)
self.resetted = False
self.finished.wait(self.interval)
if not self.finished.isSet():
self.function(*self.args, **self.kwargs)
self.finished.set()
print "Time: %s - timer finished!" % time.asctime()
def reset(self, interval=None):
""" Reset the timer """
if interval:
print "Time: %s - timer resetting to %.2f..." % (time.asctime(), interval)
self.interval = interval
else:
print "Time: %s - timer resetting..." % time.asctime()
self.resetted = True
self.finished.set()
self.finished.clear()
#
# Usage examples
#
def hello():
print "Time: %s - hello, world" % time.asctime()
# No reset
print "Time: %s - start..." % time.asctime()
tim = TimerReset(5, hello)
tim.start()
print "Time: %s - sleeping for 10..." % time.asctime()
time.sleep (10)
print "Time: %s - end..." % time.asctime()
print "\n\n"
# With Reset
print "Time: %s - start..." % time.asctime()
tim = TimerReset(5, hello)
tim.start()
print "Time: %s - sleeping for 4..." % time.asctime()
time.sleep (4)
tim.reset()
print "Time: %s - sleeping for 10..." % time.asctime()
time.sleep (10)
print "Time: %s - end..." % time.asctime()
print "\n\n"
# With reset interval
print "Time: %s - start..." % time.asctime()
tim = TimerReset(5, hello)
tim.start()
print "Time: %s - sleeping for 4..." % time.asctime()
time.sleep (4)
tim.reset (9)
print "Time: %s - sleeping for 10..." % time.asctime()
time.sleep (10)
print "Time: %s - end..." % time.asctime()
#
# Output of test program
#
Time: Fri Sep 24 15:31:16 2010 - start...
Time: Fri Sep 24 15:31:16 2010 - timer running...
Time: Fri Sep 24 15:31:16 2010 - timer waiting for timeout in 5.00...
Time: Fri Sep 24 15:31:16 2010 - sleeping for 10...
Time: Fri Sep 24 15:31:21 2010 - hello, world
Time: Fri Sep 24 15:31:21 2010 - timer finished!
Time: Fri Sep 24 15:31:26 2010 - end...
Time: Fri Sep 24 15:31:26 2010 - start...
Time: Fri Sep 24 15:31:26 2010 - timer running...
Time: Fri Sep 24 15:31:26 2010 - timer waiting for timeout in 5.00...
Time: Fri Sep 24 15:31:26 2010 - sleeping for 4...
Time: Fri Sep 24 15:31:30 2010 - timer resetting...
Time: Fri Sep 24 15:31:30 2010 - sleeping for 10...
Time: Fri Sep 24 15:31:30 2010 - timer waiting for timeout in 5.00...
Time: Fri Sep 24 15:31:35 2010 - hello, world
Time: Fri Sep 24 15:31:35 2010 - timer finished!
Time: Fri Sep 24 15:31:40 2010 - end...
Time: Fri Sep 24 15:31:40 2010 - start...
Time: Fri Sep 24 15:31:40 2010 - timer running...
Time: Fri Sep 24 15:31:40 2010 - timer waiting for timeout in 5.00...
Time: Fri Sep 24 15:31:40 2010 - sleeping for 4...
Time: Fri Sep 24 15:31:44 2010 - timer resetting to 9.00...
Time: Fri Sep 24 15:31:44 2010 - sleeping for 10...
Time: Fri Sep 24 15:31:44 2010 - timer waiting for timeout in 9.00...
Time: Fri Sep 24 15:31:53 2010 - hello, world
Time: Fri Sep 24 15:31:53 2010 - timer finished!
Time: Fri Sep 24 15:31:54 2010 - end...
|
After searching in the internet for sometime, and not satisified with the existing Resettable Timer class. So, took the initiative to look at builtin python Timer code and add an enhancement to support reset function, as well as changing the timer interval time while calling reset.
This seems not to work: once the timer stopped running, it can't be restarted whatsoever. Is there a way to make a timer restart after it's finished?
The Timer is based on Thread. The principle of Thread is, once it is started and completed/finished, then the thread is gone/dead.
Although a modification of it can be: by modifying the run() to not to exit the run function, in other words an infinite loop. In this infinite loop, you keep checking if timer is on or off, and act accordingly.