asyncio coroutine

 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
import asyncio
@asyncio.coroutine
def waiting(r):
print("hello from waiting -", r)
yield from asyncio.sleep(2)
print("bye from waiting -", r)
return r
@asyncio.coroutine
def serial():
a = yield from waiting(1)
b = yield from waiting(2)
c = yield from waiting(a + b)
print(c)
@asyncio.coroutine
def parallel():
a, b = yield from asyncio.gather(waiting(1), waiting(2))
c = yield from waiting(a + b)
print(c)
@asyncio.coroutine
def postponed():
a = yield from waiting(1)
b = yield from waiting(2)
asyncio.async(waiting('after'))
return a + b
@asyncio.coroutine
def pp_caller():
r = yield from postponed()
print("got result from postponed -", r)
yield from asyncio.sleep(3)
loop.stop()
loop = asyncio.get_event_loop()
print("-- serial")
loop.run_until_complete(serial())
print("-- parallel")
loop.run_until_complete(parallel())
print("-- postponed")
asyncio.Task(pp_caller())
loop.run_forever()
loop.close()