-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy path12_asyncio.py
51 lines (36 loc) · 1005 Bytes
/
12_asyncio.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
#Install these packages first.
#pip install --upgrade pip aiohttp aiofiles
#Sequential program example
import time
def make_coffee():
print("Start Making Coffee...")
time.sleep(10)
print("Finish Making Coffee...")
def make_toast():
print("Start making toast...")
time.sleep(10)
print("Finish making toast...")
def morning_routine():
make_coffee()
make_toast()
t1 = time.perf_counter()
morning_routine()
elapsed = time.perf_counter() - t1
print(f"Time Taken: {elapsed:0.2f} seconds.")
#async program example
import asyncio
import time
async def make_coffee():
print("Start Making Coffee...")
await asyncio.sleep(10)
print("Finish Making Coffee...")
async def make_toast():
print("Start making toast...")
await asyncio.sleep(10)
print("Finish making toast...")
async def morning_routine():
await asyncio.gather(make_coffee(), make_toast())
t1 = time.perf_counter()
asyncio.run(morning_routine())
elapsed = time.perf_counter() - t1
print(f"Time Taken: {elapsed:0.2f} seconds.")