-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
64 lines (49 loc) · 1.69 KB
/
example.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 events import Event, EventDispatcher, EventListener, EventDispatchManager
from time import sleep
from random import randrange
import os
class ExampleEvent1(Event):
def __init__(self, msg):
self.msg = msg
self.id = id(self)
class ExampleEvent2(Event):
def __init__(self, msg):
self.type = 'ExampleEvent2'
self.msg = msg
self.id = id(self)
# Example dispatcher
class ExampleEventDispatcher(EventDispatcher):
def __init__(self):
super().__init__(ExampleEvent1, ExampleEvent2)
def init(self):
# Use mutable object if values need to be mutated.
return (), {'count': [0]}
def update(self, count):
count[0] += 1
res = randrange(0, 2)
if res == 0:
msg = '[ExampleEvent1] count={: 5d}'.format(count[0])
return ExampleEvent1(msg)
elif res == 1:
msg = '[ExampleEvent2] count={: 5d}'.format(count[0])
return ExampleEvent2(msg)
def finish(self, count):
print('Finishing . . .')
class ExampleEventListener(EventListener):
def __init__(self):
callbacks = {ExampleEvent1: self.on_example1, ExampleEvent2: self.on_example2}
super().__init__(callbacks)
def on_example1(self, event):
print('{}, eventID={}'.format(event.msg, event.id))
def on_example2(self, event):
print('{}, eventID={}'.format(event.msg, event.id))
if __name__ == '__main__':
listener = ExampleEventListener()
disp = ExampleEventDispatcher()
mgr = EventDispatchManager(disp, listener)
mgr.deploy_dispatchers()
for i in range(10):
sleep(0.5)
mgr.invoke_dispatch()
mgr.end_dispatchers()
print('DONE!')