-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10_asyncio.py
More file actions
42 lines (29 loc) · 1 KB
/
10_asyncio.py
File metadata and controls
42 lines (29 loc) · 1 KB
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
import asyncio
from time import time, sleep
""" NOTE: Tested with Python 3.11 """
async def async_count_example(n: int, job: str):
for i in range(1, n + 1):
print(f"{job}: {i}")
await asyncio.sleep(0.5)
async def run_async_count_example():
start = time()
job1 = asyncio.create_task(async_count_example(3, "First"))
job2 = asyncio.create_task(async_count_example(2, "Second"))
job3 = asyncio.create_task(async_count_example(1, "Third"))
await asyncio.wait({job1, job2, job3})
print(f"AsyncIO spent {time() - start} Seconds")
def sync_count_example(n: int, job: str):
for i in range(1, n + 1):
print(f"{job}: {i}")
sleep(0.5)
def run_sync_count_example():
start = time()
sync_count_example(3, "First")
sync_count_example(2, "Second")
sync_count_example(1, "Third")
print(f"Sync spent {time() - start} Seconds")
def main():
run_sync_count_example()
asyncio.run(run_async_count_example())
if __name__ == "__main__":
main()