Different way to create a thread in Python - GeeksforGeeks

01 Oct, 2020
A thread is an entity within a process that can be scheduled for execution. Also, it is the smallest unit of processing that can be performed in an Operating System. 
1) Create a Thread without using an Explicit function: 
By importing the module and creating the Thread class object separately we can easily create a thread. It is a function-oriented way of creating a thread. 
Python3
# Import required modulesfrom threading import *        # Explicit functiondef display() :                  for i in range(10) :    print("Child Thread")    # Driver Code          # Create object of thread class    Thread_obj = Thread(target=display)          # Executing child threadThread_obj.start()              # Executing main threadfor i in range(10):              print('Main Thread')
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread  

In the above example, we have created an explicit function display() which prints Child Thread 10 times. Then we created a Thread class object named Thread_obj using the threading module. The first Thread is targeting the display() method i.e. display() method will be executed by this Thread object and the main thread(the second one) is targeting the for loop and will be responsible for executing by the Main Thread 10 times.
NOTE: Here which thread will get chance first (Main Thread or Child Thread) depends on the Thread Scheduler present in Python Virtual Machine (PVM), so we can’t predict the output. 
2) Create Thread by extending Thread Class : 
In this method, we will extend the thread class from the threading module. This approach of creating a thread is also known as Object-Oriented Way.  
Python3
# Import required modulefrom threading import *    # Extending Thread classclass Mythread(Thread):      # Target function for thread    def run(self):        for i in range(10):            print('Child Thread')    # Driver Code              # Creating thread class objectt = Mythread()  # Execution of target functiont.start()  # Executed by main threadfor i in range(10):    print('Main Thread')
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread

In the above example, we create a class that extends the Thread class, inside this class, we must override the run() method which will be the target function of our Child Thread, when the start() method is called it initiates the execution of the run() method(Target Function) of the thread. 
3. Create Thread without extending Thread Class : 
Another Object-Oriented Way of creating Thread is by not extending any thread class to create Thread.
Python3
# Import required modulesfrom threading import *    # Creating classclass Gfg:      # Creating instance method    def method1(self):        for i in range(10):            print('Child Thread')    # Driver Code              # Creating object of Gfg classobj = Gfg()  # Creating object of thread by# targeting instance method of Gfg classthread_obj = Thread(target=obj.method1)  # Call the target function of threathread_obj.start()  # for loop executed by main threadfor i in range(10):    print('Main Thread')
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread

In the above program, we separately create an object of thread class and Gfg class, and whenever we create an object of thread class that time we have to mention the target function as well. The thread class object targets the instance method of the Gfg class. To start the execution of the target function we must call the start() method.
Python-threading
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | os.path.join() method
Create a directory in Python
Defaultdict in Python
Python | Get unique values from a list
Python | Pandas dataframe.groupby()