forked from chyld/asyncio-summer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coroutine.py
47 lines (33 loc) · 1.08 KB
/
coroutine.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
import asyncio
import time
from random import randint
def sync_randn():
time.sleep(3)
return randint(1, 10)
def main():
start = time.perf_counter()
r = sync_randn()
elapsed = time.perf_counter() - start
print(f"sync {r} took {elapsed:0.2f} seconds.")
# this is a coroutine
async def async_randn():
await asyncio.sleep(3)
return randint(1, 10)
async def async_main():
start = time.perf_counter()
r = await async_randn()
elapsed = time.perf_counter() - start
print(f"async {r} took {elapsed:0.2f} seconds.")
start = time.perf_counter()
r = await asyncio.gather(async_randn(), async_randn(), async_randn())
elapsed = time.perf_counter() - start
print(f"async {r} took {elapsed:0.2f} seconds.")
start = time.perf_counter()
# use generator comprehension and unpack with *
r = await asyncio.gather(*(async_randn() for _ in range(10)))
elapsed = time.perf_counter() - start
print(f"async {r} took {elapsed:0.2f} seconds.")
if __name__ == "__main__":
main()
# create event loop
asyncio.run(async_main())