-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreading-demo1.py
33 lines (27 loc) · 1.3 KB
/
Threading-demo1.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
import time
import threading # importing the threading module (older way)
start = time.perf_counter()
# Any Function which is consuming some amount of time
def do_something(seconds) :
print(f'Sleeping {seconds} seconds....thread ')
time.sleep(seconds)
print('Done Sleeping...thread ')
# Now callling the functions directly (naive method)
# do_something(1)
# do_something(1)
# t1 = threading.Thread(target=do_something, args=[1])
# t2 = threading.Thread(target=do_something, args=[1])
# t1.start()
# t2.start()
# t1.join() # .join will just make sure that after all your threads are executed then halt the main program and after
# t2.join() # execution of all the threads then resume the rest of the program. ie... count the total time taken after all your threads are completed fully
threadslist = []
for _ in range(10):
t = threading.Thread(target=do_something, args=[2]) # This is a generic line of code to create a thread. Now loop on this
t.start()
threadslist.append(t) # keep on appending to a list as the threads are starting one by one
# Now loop through that threads list and join all those so that the program will end once all the processes are done executing!
for thread in threadslist:
thread.join()
finish= time.perf_counter()
print(f'finished in {round(finish-start,2)} seconds')