-
Notifications
You must be signed in to change notification settings - Fork 0
/
tick_mgr.py
62 lines (43 loc) · 1.43 KB
/
tick_mgr.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
from threading import Timer
from threading import Thread
from threading import Event
class RepeatTimer(Thread):
def __init__(self, interval, function, args=None, kwargs=None):
Thread.__init__(self)
self.interval = interval
self.function = function
self.args = args if args is not None else []
self.kwargs = kwargs if kwargs is not None else {}
self.finished = Event()
def cancel(self):
self.finished.set()
def run(self):
while not self.finished.wait(self.interval):
self.function(*self.args, **self.kwargs)
g_setTimerObj = set()
def RegisterTick(_, nTickTime, CallBack, *tParam):
TimerObj = RepeatTimer(nTickTime / 1000.0, CallBack, tParam)
TimerObj.start()
g_setTimerObj.add(TimerObj)
return TimerObj
RegisterNotFixTick = RegisterTick
def RegisterOnceTick(_, nTickTime, CallBack, *tParam):
TimerObj = Timer(nTickTime / 1000.0, CallBack, tParam)
TimerObj.start()
g_setTimerObj.add(TimerObj)
return TimerObj
def UnRegisterTick(nTickID):
if nTickID is None:
return
TimerObj = nTickID
g_setTimerObj.discard(TimerObj)
TimerObj.cancel()
def IsExistTickID(nTickID):
if not nTickID:
return False
return nTickID in g_setTimerObj
if __name__ == '__main__':
def f(a, b):
print("f:", a, b)
t1 = RegisterTick(None, 100, f, 1, 2)
t2 = RegisterOnceTick(None, 450, UnRegisterTick, t1)