-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedco.py
64 lines (53 loc) · 1.4 KB
/
schedco.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
# schedco.py
#
# A coroutine-based messaging system loosely based on actors.
from queue import Queue
# Dictionary mapping task names to coroutines
tasks = { }
# Queue of runnable tasks and pending messages
task_queue = Queue()
# Register a new coroutine
def register(name, task):
tasks[name] = task
# Send a message to a task
def send(name,msg):
task = tasks.get(name)
if task:
# Put the task and message on queue
task_queue.put((task,msg))
# Run all tasks
def scheduler():
while not task_queue.empty():
# Get the next task and message
task, msg = task_queue.get()
# Send the message to the task
try:
task.send(msg)
except StopIteration:
pass
if __name__ == '__main__':
from coroutine import coroutine
@coroutine
def printer():
while True:
msg = yield
print(msg)
@coroutine
def ping():
while True:
msg = yield
send("printer",msg)
send("pong","Hello from ping")
@coroutine
def pong():
while True:
msg = yield
send("printer",msg)
send("ping","Hello from pong")
register("printer", printer())
register("pong",pong())
register("ping",ping())
# Send an initial message to initiate execution
send("ping","starting")
# Run the scheduler
scheduler()