-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultithreading.py
More file actions
40 lines (30 loc) · 943 Bytes
/
multithreading.py
File metadata and controls
40 lines (30 loc) · 943 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
39
40
# multithreading = Used to perform multiple task concurrently (multitasking)
# Good for I/O bound tasks like reading files or fetching data from APIs
# threading.thread(target=my_function)
import threading
import time
def walk_dog(first): # Expecting a single argument
time.sleep(1)
print(f"You finish walking the {first}")
def clean_house():
time.sleep(3)
print(f"You finish cleaning the house")
def get_groceries():
time.sleep(5)
print(f"You finish getting groceries")
"""walk_dog()
clean_house()
get_groceries()"""
# Creating threads
chore1 = threading.Thread(target=walk_dog, args=("Scooby",))
chore2 = threading.Thread(target=clean_house)
chore3 = threading.Thread(target=get_groceries)
# Starting threads
chore1.start()
chore2.start()
chore3.start()
# Joining threads to ensure they complete
chore1.join()
chore2.join()
chore3.join()
print("All chores are completed.")