-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathexample_5.py
More file actions
28 lines (20 loc) · 598 Bytes
/
example_5.py
File metadata and controls
28 lines (20 loc) · 598 Bytes
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
import asyncio
import time
async def fetch_data(param):
print(f"Do something with {param}...")
time.sleep(param)
print(f"Done with {param}")
return f"Result of {param}"
async def main():
task1 = asyncio.create_task(fetch_data(1))
task2 = asyncio.create_task(fetch_data(2))
result1 = await task1
print("Task 1 fully completed")
result2 = await task2
print("Task 2 fully completed")
return [result1, result2]
t1 = time.perf_counter()
results = asyncio.run(main())
print(results)
t2 = time.perf_counter()
print(f"Finished in {t2 - t1:.2f} seconds")