-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathexample_2.py
More file actions
28 lines (20 loc) · 623 Bytes
/
example_2.py
File metadata and controls
28 lines (20 loc) · 623 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}...")
await asyncio.sleep(param)
print(f"Done with {param}")
return f"Result of {param}"
async def main():
task1 = fetch_data(1) # Could be awaited directly
task2 = fetch_data(2) # Could be awaited directly
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")