forked from phaustin/async_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_coroutines2.py
36 lines (27 loc) · 905 Bytes
/
test_coroutines2.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
#http://antroy.blogspot.ca/2007/04/python-coroutines.html
from __future__ import print_function
def rota(people):
_people = list(people)
current = 0
while len(_people):
command = yield _people[current]
current = (current + 1) % len(_people)
if command:
comm, name = command
if comm == "add":
_people.append(name)
elif comm == "remove" and name in _people:
_people.remove(name)
def printname(name):
print("It's %s's turn." % name)
if __name__ == "__main__":
people = ["Ant", "Bernard", "Carly", "Deb", "Englebert"]
r = rota(people)
for i in range(6):
printname(next(r))
printname(r.send(("add", "Fred")))
for i in range(7):
printname(next(r))
printname(r.send(("remove","Deb")))
for i in range(6):
printname(next(r))