How to do it...

Let's have a look at the following steps:

  1. To determine which thread is running, we create three target functions and import the time module to introduce a suspended execution of two seconds:
import threading
import time

def function_A():
print (threading.currentThread().getName()+str('-->
starting '))
time.sleep(2)
print (threading.currentThread().getName()+str( '-->
exiting '))

def function_B():
print (threading.currentThread().getName()+str('-->
starting '))
time.sleep(2)
print (threading.currentThread().getName()+str( '-->
exiting '))

def function_C():
print (threading.currentThread().getName()+str('-->
starting '))
time.sleep(2)
print (threading.currentThread().getName()+str( '-->
exiting '))
  1. Three threads are instantiated with a target function. Then, we pass the name that is to be printed and, if it is not defined, then the default name will be used. Then, the start() and join() methods are called for each thread:
if __name__ == "__main__":

t1 = threading.Thread(name='function_A', target=function_A)
t2 = threading.Thread(name='function_B', target=function_B)
t3 = threading.Thread(name='function_C',target=function_C)

t1.start()
t2.start()
t3.start()

t1.join()
t2.join()
t3.join()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.104.238