-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync.py
More file actions
62 lines (48 loc) · 2.15 KB
/
async.py
File metadata and controls
62 lines (48 loc) · 2.15 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
Async is called as Non-Blockin-I/O-bound. A single thread runs many tasks
Example: One person is downloading all images. While waiting for network for image 1, switches to image 2, then 3, etc. One worker handles everything efficiently. One worker, switches between tasks while waiting.
Pros: Memory efficient. Handles thousands of task all together easily.
"""
import asyncio # this is in-built library to use async operations
import time
import requests
# normal process of function calling
def send_email(user):
print(f"Sending to {user}...")
time.sleep(3)
print(f"Done {user}")
def main():
send_email("User A") # this will run
send_email("User B") # this will run once User A finishes
send_email("User C") # this will run once User B finishes
# main()
# use Async for calling function
async def send_email(user):
print(f"Sending to {user}...")
await asyncio.sleep(3) # await means: “Wait here until this task finishes.” asyncio.sleep will not block the task, whereas time.sleep will block the task. this is Simulate delay i.e takes time to send email
print(f"Done {user}")
async def main():
await asyncio.gather(
send_email("User A"), # this will run at same time all together, no waiting
send_email("User B"), # this will run at same time all together, no waiting
send_email("User C"), # this will run at same time all together, no waiting
)
asyncio.run(main())
# use Async to download multiple images faster
url = 'https://picsum.photos/2000/3000'
def download_image_blocking(index):
print(f"Downloading image {index}...")
r = requests.get(url)
with open(f"target/image_{index}.jpg", "wb") as f:
f.write(r.content)
print(f"Finished image {index}")
async def download_image_async(index):
# Run blocking function in a separate thread without blocking event loop
await asyncio.to_thread(download_image_blocking, index)
async def main_download():
tasks = [download_image_async(i) for i in range(10)]
await asyncio.gather(*tasks)
start_time = time.time()
asyncio.run(main_download())
end_time = time.time()
print(f"Total time taken: {end_time - start_time:.2f} seconds")