forked from uscki/robot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventBus.py
49 lines (41 loc) · 893 Bytes
/
EventBus.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
import threading
evt = threading.Event()
threads = []
clients = {}
# Events are just names, dude
class ChatEvent(): pass
class ImageEvent(): pass
def add(bot):
t = threading.Thread(target=bot.run)
t.bot = bot
threads.append(t)
t.start()
def addClient(name, c):
clients[name] = c
def client(name):
return clients[name]
def kill():
trigger(None)
for t in threads:
t.bot.kill()
t.join()
for c in clients:
c.end()
def trigger(obj):
evt.value = obj
evt.set()
evt.clear()
def awaitall():
evt.wait()
obj = evt.value
if obj:
return obj
else:
# this is really not robust
# if a bot is not waiting, it will never get the kill signal
raise threading.ThreadError()
def await(event_type):
e = None
while not isinstance(e, event_type):
e = awaitall()
return e