Home » Python » Thread Object
Welcome to another tutorial, here we will learn about Thread class and its object in Python. The Thread class is the most commonly used threading module, as it is primarily used to create and run threads. It also, provides all the major functionalities need to create and manage a thread.
The thread objects are the objects of the Thread class in which each object represents an activity to be performed in a separate thread of control in a given program.
We have two ways to create the Thread object and specify the activity to be performed.
Thread object that is created by making use of the constructor or run method can be started by using the start() method. In addition, whenever a Thread object starts a new thread, then internally the method run() is invoked.
Let’s check out the example below:
import time
import threading
def thread1(i):
time.sleep(3)
print('No. printed by Thread 1: %d' %i)
def thread2(i):
print('No. printed by Thread 2: %d' %i)
if __name__ == '__main__':
t1 = threading.Thread(target=thread1, args=(10,))
t2 = threading.Thread(target=thread2, args=(12,))
# start the threads
t1.start()
t2.start()
# join the main thread
t1.join()
t2.join()
Output:
No. printed by Thread 2: 12
No. printed by Thread 1: 10
From the example, we have used the time module to make one of the threads sleep.
Below is the basic syntax of the Thread class constructor:
Thread(group=None, target=None, name=None, args=(), kwargs={})
In the next session, we will discuss how it works.
If we initialize a thread by making use of the Thread class constructor, so start() method must be called to start the thread.
If the thread starts, the thread is considered alive and active, but if its run() method terminates, either normally, or due to an unhandled exception then the thread stops being alive or active. Therefore, the isAlive() method can be used to test if the thread is alive or not at any given point in time.
Also, the thread's join() method is called to join any thread. It, however, blocks the calling thread until the thread whose join() method was called is terminated.
In the above example, from the main thread, we call t1.join() and t2.join(), therefore the main thread will wait for the threads t1 and t2 to terminate and then end.
All threads have a name associated with them, and that name can be passed to the constructor, or we can set or retrieve the name by using setname() and getname() methods accordingly.
A flag daemon thread could as well be associated with any thread. The importance of this flag is because the entire Python program exits when only daemon threads are left. You can set or retrieve the flag by using setDaemon() method and getDaemon() method respectively.
Note that the main thread object corresponds to the initial thread of control in the python program, and it is not a daemon thread.
Let's try to understand the importance of the methods provided by the Thread class along with our code.
Below is the basic syntax of the Thread class constructor:
Thread(group=None, target=None, name=None, args=(), kwargs={})
The constructor allows many arguments, a few of which are required and some are not. Below is a highlight of what they look like:
The start() method is used to start the thread's activity. If this method is called, internally the run() method is invoked and executes the target function or the callable object.
It is the Method representing the thread's activity. It can be overridden in a subclass extending the thread class of the threading module. However, the standard run() method invokes the callable object passed to the object's constructor as the target argument with sequential and keyword arguments obtained from the args and kwargs arguments, respectively.
Check out the example.
import threading
import time
class MyThread(threading.Thread):
# overriding constructor
def __init__(self, i):
# calling parent class constructor
threading.Thread.__init__(self)
self.x = i
# define your own run method
def run(self):
print("Value stored is: ", self.x)
time.sleep(3)
print("Exiting thread with value: ", self.x)
thread1 = MyThread(1)
thread1.start()
thread2 = MyThread(2)
thread2.start()
Output:
Value stored is: 1
Value stored is: 2
Exiting thread with value: 1
Exiting thread with value: 2
If we call this method for any thread, it blocks the calling thread until the thread whose the method join() is called terminates, either normally or through an unhandled exception.
Now, when you want to provide the timeout argument, it should be a floating point number.
The method is used to return the thread's name.
The method is used for setting the thread's name. The name is a string data type used for only identification purposes.
The isAlive() method returns whether the thread is alive or not. Just like said earlier, a thread is alive from the moment the start() method returns until its method run() terminates.
The method is typically used to get the value of the thread's daemon flag.
This method is typically used to set the thread's daemon flag to the Boolean value i.e. daemonic. But it must be called before the start() method is called.
In addition, the Python program exists in cases where no active non-daemon thread is left.