{"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":"

Creating a Thread using Class in Python<\/h2>\n

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

Extend Thread class to create Threads :<\/h3>\n

Let’s create a FileLoadTh<\/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

When we start thread by calling 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

It is better to wait for other threads to finish calling 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

Now let’s create a thread that executes 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

So both 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

 <\/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>\n

Create a Thread using Class in Python<\/span> Read More »<\/a><\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nCreate a Thread using Class in Python - Python Programs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/python-programs.com\/create-a-thread-using-class-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Create a Thread using Class in Python - Python Programs\" \/>\n<meta property=\"og:description\" content=\"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 … Create a Thread using Class in Python Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/create-a-thread-using-class-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-01T09:57:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-10T06:41:35+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Satyabrata Jena\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/create-a-thread-using-class-in-python\/#webpage\",\"url\":\"https:\/\/python-programs.com\/create-a-thread-using-class-in-python\/\",\"name\":\"Create a Thread using Class in Python - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2023-11-01T09:57:12+00:00\",\"dateModified\":\"2023-11-10T06:41:35+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/create-a-thread-using-class-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/create-a-thread-using-class-in-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/create-a-thread-using-class-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Create a Thread using Class in Python\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/create-a-thread-using-class-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/create-a-thread-using-class-in-python\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd\"},\"headline\":\"Create a Thread using Class in Python\",\"datePublished\":\"2023-11-01T09:57:12+00:00\",\"dateModified\":\"2023-11-10T06:41:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/create-a-thread-using-class-in-python\/#webpage\"},\"wordCount\":201,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/create-a-thread-using-class-in-python\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd\",\"name\":\"Satyabrata Jena\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g\",\"caption\":\"Satyabrata Jena\"},\"url\":\"https:\/\/python-programs.com\/author\/satyabrata\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Create a Thread using Class in Python - Python Programs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/python-programs.com\/create-a-thread-using-class-in-python\/","og_locale":"en_US","og_type":"article","og_title":"Create a Thread using Class in Python - Python Programs","og_description":"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 … Create a Thread using Class in Python Read More »","og_url":"https:\/\/python-programs.com\/create-a-thread-using-class-in-python\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2023-11-01T09:57:12+00:00","article_modified_time":"2023-11-10T06:41:35+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Satyabrata Jena","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/create-a-thread-using-class-in-python\/#webpage","url":"https:\/\/python-programs.com\/create-a-thread-using-class-in-python\/","name":"Create a Thread using Class in Python - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2023-11-01T09:57:12+00:00","dateModified":"2023-11-10T06:41:35+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/create-a-thread-using-class-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/create-a-thread-using-class-in-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/create-a-thread-using-class-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Create a Thread using Class in Python"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/create-a-thread-using-class-in-python\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/create-a-thread-using-class-in-python\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd"},"headline":"Create a Thread using Class in Python","datePublished":"2023-11-01T09:57:12+00:00","dateModified":"2023-11-10T06:41:35+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/create-a-thread-using-class-in-python\/#webpage"},"wordCount":201,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/create-a-thread-using-class-in-python\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd","name":"Satyabrata Jena","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g","caption":"Satyabrata Jena"},"url":"https:\/\/python-programs.com\/author\/satyabrata\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/7054"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=7054"}],"version-history":[{"count":1,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/7054\/revisions"}],"predecessor-version":[{"id":7055,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/7054\/revisions\/7055"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=7054"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=7054"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=7054"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}