forked from phaustin/async_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_coroutines.py
56 lines (48 loc) · 1.37 KB
/
test_coroutines.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
#http://blog.thumbtack.net/python-coroutines/
def coroutine(f):
def wrapper(*arg, **kw):
c = f(*arg, **kw)
c.send(None)
return c
return wrapper
@coroutine
def logger(prefix="", next=None):
while True:
message = yield
print("{0}: {1}".format(prefix, message))
if next:
next.send(message)
@coroutine
def cache_checker(cache, onsuccess=None, onfail=None):
while True:
request = yield
if request in cache and onsuccess:
onsuccess.send(cache[request])
elif onfail:
onfail.send(request)
@coroutine
def load_balancer(*workers):
while True:
for worker in workers:
request = yield
worker.send(request)
@coroutine
def worker(cache, response, next=None):
while True:
request = yield
cache[request] = response
if next:
next.send(response)
cache = {}
response_logger = logger("Response")
cluster = load_balancer(
logger("Worker 1", worker(cache, 1, response_logger)),
logger("Worker 2", worker(cache, 2, response_logger)),
logger("Worker 3", worker(cache, 3, response_logger)),
)
cluster = cache_checker(cache, response_logger, cluster)
cluster = logger("Request", cluster)
if __name__ == "__main__":
from random import randint
for i in range(20):
cluster.send(randint(1, 5))