-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_listener.1.py
76 lines (58 loc) · 1.74 KB
/
event_listener.1.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
65
66
67
68
69
70
71
72
73
74
75
76
import threading
import time
class eventListener:
works = dict()
def __init__(self, refresh_rate=0.01):
self.refresh_rate = refresh_rate
self.thread = threading.Thread(target=self.run, daemon=True)
self.thread.start()
pass
def run(self):
# print(time.perf_counter())
while True:
start = time.perf_counter()
for _, work in self.works.items():
hasEvent, param_dict = work[0](**work[2])
if hasEvent:
return_dict = dict(work[3])
if(len(param_dict)>0):
return_dict.update(param_dict)
threading.Thread(target=work[1], kwargs=return_dict).start()
print(time.perf_counter()-start)
time.sleep(self.refresh_rate)
def addListener(self, check, callback,
args_check=dict(), args_call=dict()):
content = (check, callback, args_check, args_call)
key = (id(check), id(callback), id(args_check),
id(args_call))
self.works[key] = content
return key
def removeListener(self, key):
try:
self.works.pop(key)
except KeyError as e:
print(key, "is not present")
pass
pass
class eventScheduler:
def __init__(self):
pass
e = eventListener(1)
i=0
loop=True
def check(a, b):
global i
i += 1
# print(a ,b)
if(i==4):
return True, {"a":1, "b":2}
else :
return False, {"a":1, "b":2}
def callback(a, b):
print("event fired")
# print(a ,b)
global loop
loop=False
e.addListener(check, callback, {"a":1, "b":2})
while loop:
time.sleep(1)