How to determine the current thread
Using arguments to identify or name the thread is cumbersome and unnecessary. Each Thread instance has a name with a default value that can be changed as the thread is created. Naming threads is useful in server processes with multiple service threads that handle different operations.
How to do it…
To determine which thread is running, we create three target functions and import the time module to introduce a suspend execution of two seconds:
import threading
import time
def first_function():
print (threading.currentThread().getName()+\
str(' is Starting \n'))
time.sleep(2)
print (threading.currentThread().getName()+\
str( ' is Exiting \n'))
return
def second_function():
print (threading.currentThread().getName()+\
str(' is Starting \n'))
time.sleep(2)
print (threading.currentThread().getName()+\
str( ' is Exiting \n'))
return
def...
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime