Creating a Thread using Class in Python
In this article, we will discuss how we can create thread in python by extending class from Thread class or calling a member function of the class.
Python provides a threading module to create and manage threads.
Extend Thread class to create Threads :
Let’s create a FileLoadTh
class by extending Thread class provided by the threading module where it mimics functionality of a File loader and it’s run()
method sleeps for sometime.
When we start thread by calling start()
function, it will invoke run() method of Thread class and overriden run()
method will be executed which may contain any custom implementation.
It is better to wait for other threads to finish calling join()
method on FileLoaderTh
class object before returning from main thread.
from threading import Thread import time # FileLoadTh extending Thread class class FileLoaderTh(Thread): def __init__(self, fileName, encryptionType): # Calling Thread class's init() function Thread.__init__(self) self.fileName = fileName self.encryptionType = encryptionType # Overriding run() of Thread class def run(self): print('Loading started from file : ', self.fileName) print('Encryption Type : ', self.encryptionType) for i in range(5): print('Please wait loading... ') time.sleep(3) print('Loading finished from file : ', self.fileName) def main(): # Create an object of Thread th = FileLoaderTh('threadDemo.csv','DOC') # calling Thread class start() to start the thread th.start() for i in range(5): print('Inside main function-------') time.sleep(3) # Wait for thread to finish th.join() if __name__ == '__main__': main()
Output : Loading started from file :Â threadDemo.csv Inside main function------- Encryption Type :Â DOC Please wait loading... Inside main function------- Please wait loading... Inside main function------- Please wait loading... Please wait loading... Inside main function------- Please wait loading... Inside main function------- Loading finished from file :Â threadDemo.csv
Create a Thread from a member function of a class :
Now let’s create a thread that executes loadcont()
memeber function of FileLoadTh
class. For that we can create a object and then pass the function with object to target argument of Thread class constructor.
So both main()
function and loadcont()
member function will run in parallel and at the end main() function will wait for other threads calling join()
function on the same object.
import threading import time class FileLoaderTh(): def __init__(self): pass def loadcont(self, fileName, encryptionType): print('Loading started from file : ', fileName) print('Encryption Type : ', encryptionType) for i in range(5): print('Loading ... ') time.sleep(3) print('Loading finished from file : ', fileName) def main(): # Creating object of FileLoaderTh class fileLoader = FileLoaderTh() # Create a thread using member function of FileHolderTh class th = threading.Thread(target=fileLoader.loadcont, args=('threadDemo.csv','DOC', )) # Start a thread th.start() # Print some logs in main thread for i in range(5): print('Inside main Function') time.sleep(3) # Wait for thread to finish th.join() if __name__ == '__main__': main()
Output : Loading started from file :Â threadDemo.csv Encryption Type :Â DOC Inside main Function Loading ... Inside main Function Loading ... Inside main Function Loading ... Inside main Function Loading ... Loading ... Inside main Function Loading finished from file :Â threadDemo.csv