{"id":7054,"date":"2023-11-01T15:27:12","date_gmt":"2023-11-01T09:57:12","guid":{"rendered":"https:\/\/python-programs.com\/?p=7054"},"modified":"2023-11-10T12:11:35","modified_gmt":"2023-11-10T06:41:35","slug":"create-a-thread-using-class-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/create-a-thread-using-class-in-python\/","title":{"rendered":"Create a Thread using Class in Python"},"content":{"rendered":"
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.<\/p>\n
Python provides a threading module to create and manage threads.<\/p>\n
Let’s create a When we start thread by calling It is better to wait for other threads to finish calling Now let’s create a thread that executes So both <\/p>\n","protected":false},"excerpt":{"rendered":" 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 …<\/p>\nFileLoadTh<\/code> class by extending Thread class provided by the threading module where it mimics functionality of a File loader and it’s
run()<\/code> method sleeps for sometime.<\/p>\n
start()<\/code> function, it will invoke run() method of Thread class and overriden
run()<\/code> method will be executed which may contain any custom implementation.<\/p>\n
join()<\/code> method on
FileLoaderTh<\/code> class object before returning from main thread.<\/p>\n
from threading import Thread\r\nimport time\r\n# FileLoadTh extending Thread class\r\nclass FileLoaderTh(Thread):\r\n def __init__(self, fileName, encryptionType):\r\n # Calling Thread class's init() function\r\n Thread.__init__(self)\r\n self.fileName = fileName\r\n self.encryptionType = encryptionType\r\n # Overriding run() of Thread class\r\n def run(self):\r\n print('Loading started from file : ', self.fileName)\r\n print('Encryption Type : ', self.encryptionType)\r\n for i in range(5):\r\n print('Please wait loading... ')\r\n time.sleep(3)\r\n print('Loading finished from file : ', self.fileName)\r\ndef main():\r\n # Create an object of Thread\r\n th = FileLoaderTh('threadDemo.csv','DOC')\r\n # calling Thread class start() to start the thread\r\n th.start()\r\n for i in range(5):\r\n print('Inside main function-------')\r\n time.sleep(3)\r\n # Wait for thread to finish\r\n th.join()\r\nif __name__ == '__main__':\r\n main()\r\n<\/pre>\n
Output :\r\nLoading started from file :\u00a0 threadDemo.csv\r\nInside main function-------\r\nEncryption Type :\u00a0 DOC\r\nPlease wait loading...\r\nInside main function-------\r\nPlease wait loading...\r\nInside main function-------\r\nPlease wait loading...\r\nPlease wait loading...\r\nInside main function-------\r\nPlease wait loading...\r\nInside main function-------\r\nLoading finished from file :\u00a0 threadDemo.csv<\/pre>\n
Create a Thread from a member function of a class :<\/h3>\n
loadcont()<\/code> memeber function of
FileLoadTh<\/code> class. For that we can create a object and then pass the function with object to target argument of Thread class constructor.<\/p>\n
main()<\/code> function and
loadcont()<\/code> member function will run in parallel and at the end main() function will wait for other threads calling
join()<\/code> function on the same object.<\/p>\n
import threading\r\nimport time\r\nclass FileLoaderTh():\r\n def __init__(self):\r\n pass\r\n \r\n def loadcont(self, fileName, encryptionType):\r\n print('Loading started from file : ', fileName)\r\n print('Encryption Type : ', encryptionType)\r\n for i in range(5):\r\n print('Loading ... ')\r\n time.sleep(3)\r\n print('Loading finished from file : ', fileName)\r\ndef main():\r\n # Creating object of FileLoaderTh class\r\n fileLoader = FileLoaderTh()\r\n # Create a thread using member function of FileHolderTh class\r\n th = threading.Thread(target=fileLoader.loadcont, args=('threadDemo.csv','DOC', ))\r\n # Start a thread\r\n th.start()\r\n # Print some logs in main thread\r\n for i in range(5):\r\n print('Inside main Function')\r\n time.sleep(3)\r\n # Wait for thread to finish\r\n th.join()\r\nif __name__ == '__main__':\r\n main()\r\n<\/pre>\n
Output :\r\nLoading started from file :\u00a0 threadDemo.csv\r\nEncryption Type :\u00a0 DOC\r\nInside main Function\r\nLoading ...\r\nInside main Function\r\nLoading ...\r\nInside main Function\r\nLoading ...\r\nInside main Function\r\nLoading ...\r\nLoading ...\r\nInside main Function\r\nLoading finished from file :\u00a0 threadDemo.csv<\/pre>\n