forked from nickglazer/python-state-machine-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventScheduler.py
More file actions
executable file
·36 lines (28 loc) · 1.03 KB
/
EventScheduler.py
File metadata and controls
executable file
·36 lines (28 loc) · 1.03 KB
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
from collections import defaultdict
import time
import queue
class EventScheduler(object):
def __init__(self):
self.subscriptions = defaultdict(list)
self.publishQ = []
self.activeObjects = []
def registerActiveObjects(self, *activeObjects):
self.activeObjects = activeObjects
def tick(self):
for event in self.publishQ:
for activeObject in self.subscriptions[event.name]:
activeObject.post(event)
self.publishQ.remove(event)
for activeObject in self.activeObjects:
activeObject.run()
async def start(self):
while(True):
self.tick()
time.sleep(0.1)
def publish(self, event):
self.publishQ.append(event)
def subscribe(self, activeObject, signal):
if activeObject not in self.subscriptions[signal] :
self.subscriptions[signal].append(activeObject)
def unsubscribe(self, activeObject, signal):
self.subscriptions[signal].remove(activeObject)