asyncio_slow: Add example on chaining coros using "yield from" from docs.

pull/11/head
Paul Sokolovsky 2014-05-01 23:55:22 +03:00
rodzic ce54259e95
commit 4a3a4927c4
1 zmienionych plików z 18 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,18 @@
#https://docs.python.org/3.4/library/asyncio-task.html#example-chain-coroutines
#import asyncio
import asyncio_slow as asyncio
@asyncio.coroutine
def compute(x, y):
print("Compute %s + %s ..." % (x, y))
yield from asyncio.sleep(1.0)
return x + y
@asyncio.coroutine
def print_sum(x, y):
result = yield from compute(x, y)
print("%s + %s = %s" % (x, y, result))
loop = asyncio.get_event_loop()
loop.run_until_complete(print_sum(1, 2))
loop.close()