kopia lustrzana https://github.com/micropython/micropython-lib
asyncio_slow: Add example on chaining coros using "yield from" from docs.
rodzic
ce54259e95
commit
4a3a4927c4
|
@ -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()
|
Ładowanie…
Reference in New Issue