forked from chyld/asyncio-summer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_theory.py
84 lines (67 loc) · 3.06 KB
/
2_theory.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Demonstrate coroutines
from random import randint
import time
import asyncio
def sync_my_randn():
# this is a blocking sleep which means no other tasks can be
# executed until this sleep has finished.
time.sleep(3)
return randint(1, 10)
# This asynchronous function is called a coroutine.
# Requires an "async" before def. Should be called with await.
# Without an await it is just a coroutine object.
async def async_my_randn():
# asyncio.sleep is an async function (coroutine) and needs to be awaited.
# this await will wait for sleep to be complete but other things can
# be executed elsewhere until this has finished waiting.
await asyncio.sleep(3)
return randint(1, 10)
def sync_main():
# Basic single call to sync_my_randn will take 3 seconds
start = time.perf_counter()
r = sync_my_randn()
elapsed = time.perf_counter() - start
print(f"{r} took {elapsed:0.2f} seconds.")
# List comprehension should take around 9 seconds
start = time.perf_counter()
r = [sync_my_randn() for _ in range(3)]
elapsed = time.perf_counter() - start
print(f"{r} took {elapsed:0.2f} seconds.")
# This is also a coroutine and needs to be awaited.
async def async_main():
# Basic single call to async_my_randn will take 3 seconds
start = time.perf_counter()
r = await async_my_randn()
elapsed = time.perf_counter() - start
print(f"{r} took {elapsed:0.2f} seconds.")
# List comprehension should also take around 9 seconds
start = time.perf_counter()
r = [await async_my_randn() for _ in range(3)]
elapsed = time.perf_counter() - start
print(f"{r} took {elapsed:0.2f} seconds.")
# Use asyncio gather to run multiple async_my_randn asynchronously or concurrently
# All 3 calls will only take 3 seconds!!
start = time.perf_counter()
# each async_my_randn() is a coroutine object it does not execute the func
r = await asyncio.gather(async_my_randn(), async_my_randn(), async_my_randn())
elapsed = time.perf_counter() - start
print(f"{r} took {elapsed:0.2f} seconds. AWESOME!!!!!!!!")
# Use asyncio gather to run multiple async_my_randn asynchronously or concurrently
# All 3 calls will only take 3 seconds!!
start = time.perf_counter()
# use * to unpack a list comprehension into separate arguments
# each task (asyncio calls them futures) is ran concurrently by gather
r = await asyncio.gather(*[async_my_randn() for _ in range(10)])
elapsed = time.perf_counter() - start
print(f"{r} took {elapsed:0.2f} seconds. AWESOMER!!!!!!!!")
# Use asyncio gather to run multiple async_my_randn asynchronously or concurrently
# All 3 calls will only take 3 seconds!!
start = time.perf_counter()
# use * to unpack a generator into separate arguments
r = await asyncio.gather(*(async_my_randn() for _ in range(10)))
elapsed = time.perf_counter() - start
print(f"{r} took {elapsed:0.2f} seconds. AWESOMER!!!!!!!!")
if __name__ == "__main__":
sync_main()
# need an event loop to run the async_main() coroutine
asyncio.run(async_main())