{"id":24038,"date":"2021-11-23T12:46:37","date_gmt":"2021-11-23T12:46:37","guid":{"rendered":"https:\/\/www.askpython.com\/?p=24038"},"modified":"2021-11-23T12:46:38","modified_gmt":"2021-11-23T12:46:38","slug":"threading-with-classes","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/oops\/threading-with-classes","title":{"rendered":"Threading With Classes In Python &#8211; A Brief Guide"},"content":{"rendered":"\n<p>This tutorial will explain to you how to build a thread in Python by utilizing classes. But first, let us define a thread.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Thread?<\/h2>\n\n\n\n<p>A thread is a parallel execution flow. This implies that your code will have two things going on at the same time.<\/p>\n\n\n\n<p>A <strong>thread <\/strong>is the smallest unit of processing for execution when a process is scheduled for execution.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of Threading in Python<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Multiple threads can run concurrently on a computer system with multiple CPUs. As a result, additional applications may run concurrently, boosting the process&#8217;s pace.<\/li><li>Input is responsive in both the situation of a single and numerous CPUs.<\/li><li>Threads have local variables.<\/li><li>When a global variable is updated in one thread, it affects the other threads as well, implying that global variable memory is shared throughout threads.<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Starting a new thread<\/h2>\n\n\n\n<p>Now that you know what a thread is, let&#8217;s look at how to build one. It is compatible with both Windows and Linux.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nthread.start_new_thread ( func, args&#x5B;, kwargs] )\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Implementing Thread using class<\/h2>\n\n\n\n<p>Now, look at the code below to understand how a thread is formed using a class.<br>The class name, in this case, is c1. Within class c1, two objects, obj, and obj1, are created.<\/p>\n\n\n\n<p>The thread is started with <strong>Obj.start()<\/strong>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; gutter: true; title: ; notranslate\" title=\"\">\nimport threading\n\nclass c1(threading.Thread) :\n    def run(self) :\n        for _ in range (2) :\n            print(threading.currentThread().getName())\nobj= c1(name=&#039;Hello&#039;)\nobj1= c1(name=&#039;Bye&#039;)\nobj.start()\nobj1.start()\n<\/pre><\/div>\n\n\n<p>The output of the code is as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nHello\nHello\nBye\nBye\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p id=\"block-d144faf1-1abb-488c-a4bd-8a80520fbf7b\">Congratulations! You just learned how to build a thread using the Python programming language. Hope you enjoyed it! &#x1f607;<\/p>\n\n\n\n<p id=\"block-4e73ecff-f7fa-46a5-be6e-35bbb0867ba8\">Liked the tutorial? In any case, I would recommend you to have a look at the tutorials mentioned below:<\/p>\n\n\n\n<ol class=\"wp-block-list\" id=\"block-39ec4c2c-ab54-45a6-804e-58eb24573b45\"><li><a href=\"https:\/\/www.askpython.com\/python\/examples\/stock-price-prediction-python\"><\/a><a href=\"https:\/\/www.askpython.com\/python\/examples\/synchronization-in-python\">Synchronization in Python \u2013 Synchronize Threads in Python<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python-modules\/daemon-threads-in-python\">Daemon Threads in Python \u2013 What Are They and How to Create Them?<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python-modules\/multithreading-in-python\">Multithreading in Python: An Easy Reference<\/a><\/li><\/ol>\n\n\n\n<p id=\"block-177e4b87-db13-427b-90fa-d70aac750e1d\">Thank you for taking your time out! Hope you learned something new!! &#x1f604;<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial will explain to you how to build a thread in Python by utilizing classes. But first, let us define a thread. What is a Thread? A thread is a parallel execution flow. This implies that your code will have two things going on at the same time. A thread is the smallest unit [&hellip;]<\/p>\n","protected":false},"author":28,"featured_media":24058,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-24038","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oops"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/24038","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=24038"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/24038\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/24058"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=24038"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=24038"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=24038"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}