-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.py
64 lines (49 loc) · 1.87 KB
/
timer.py
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
from threading import Thread, Event
def ElectionTimer(*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
self.running = True
def cancel(self):
self.running = False
self.finished.set()
def run(self):
# print "Time: %s - timer running..." % time.asctime()
while True:
if self.running:
while self.resetted:
# print "Time: %s - timer waiting for timeout in %.2f..." % (time.asctime(), self.interval)
self.resetted = False
self.finished.wait(self.interval)
self.running = False
if not self.finished.isSet():
self.function(*self.args, **self.kwargs)
# self.finished.set()
# print "Time: %s - timer finished!" % time.asctime()
def restart(self):
self.reset()
self.running = True
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.running = True
self.finished.set()
self.finished.clear()