-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync3.py
More file actions
38 lines (28 loc) · 842 Bytes
/
async3.py
File metadata and controls
38 lines (28 loc) · 842 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
29
30
31
32
33
34
35
36
37
38
# -*- coding: utf-8 -*-
"""async.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1pOZXYro3pXNPeWXYWGaqEN0IlYiecvtR
"""
import time
import asyncio
async def fetch_data(id,delay):
print("data is fetched... id:",id)
await asyncio.sleep(delay)
print("data received...id:",id)
return {"id":id,"data":"some data"}
async def main():
print("start")
task1=asyncio.create_task(fetch_data(1,2))
task2=asyncio.create_task(fetch_data(2,3))
task3=asyncio.create_task(fetch_data(3,1))
result1= await task1
print(f"data received: {result1}")
result2= await task2
print(f"data received: {result2}")
result3= await task3
print(f"data received: {result3}")
print("end")
t=time.time()
asyncio.run(main())
print(time.time()-t)