-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreads.py
More file actions
32 lines (25 loc) · 948 Bytes
/
threads.py
File metadata and controls
32 lines (25 loc) · 948 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
"""
Thread are called as Blocking-I/O-bound but runs concurrently.
example: You hire 5 people, each downloads one image at the same time. Each person still “waits” for 3 seconds, but all run concurrently. Multiple workers, each waiting for their task
Pros: good for Small number of tasks
Cons: Memory-heavy; Extra Memory uses
"""
import threading
import time
# Simulate sending email
def send_email(user):
print(f"Sending email to {user}...")
time.sleep(3) # Simulate delay (like sending real email)
print(f"Email sent to {user}")
# List of users
users = ["Alice", "Bob", "Charlie", "David"]
# Create threads for each user
threads = []
for user in users:
t = threading.Thread(target=send_email, args=[user])
threads.append(t)
t.start() # Start the thread. it runs in the background
# Wait for all threads to finish
for t in threads:
t.join() # Wait for particular thread to finish
print("All emails sent!")