-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultithreading.py
52 lines (35 loc) · 1.31 KB
/
multithreading.py
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
#multithreading= running various trask at the same time concurrently
#it is a way to run program faster like game and spotify example
import threading
import time
#time in secs
def dog_walk():
time.sleep(8)
print("walk the dog in the morning")
def get_vegetbales():
time.sleep(5)
print(" get_vegetbales from walmart")
def workout():
time.sleep(2)
print("working out")
#in cases we have parameters in our function such as
def finish_lectures(lecture1, lecture2): #passed parameters
time.sleep(7)
print(f"you fnished your lectures such as {lecture1} {lecture2}") #printing parameters
#DONOT FORGET F FOR PRINTING PARAMETERS
#taskname=threading.Thread(target=functionname)
#taskname.start()
task1=threading.Thread(target=dog_walk)
task1.start()
task2=threading.Thread(target=get_vegetbales)
task2.start()
task3=threading.Thread(target=workout)
task3.start()
task4=threading.Thread(target=finish_lectures, args=("lecture1", "lecture2",)) #accepting parameters in thread # comma at the end make sure args are tuple an is mandatory
task4.start()
#to make sure all task is finished before moving further use taskname.join()
task1.join()
task2.join()
task3.join()
task4.join()
print("all task finished!")