{"id":3951,"date":"2021-02-03T04:21:28","date_gmt":"2021-02-03T04:21:28","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=3951"},"modified":"2021-02-03T06:24:14","modified_gmt":"2021-02-03T06:24:14","slug":"multithreading-in-python","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/multithreading-in-python\/","title":{"rendered":"Multithreading in Python"},"content":{"rendered":"<p>Multitasking refers to the ability of an operating system to perform different tasks at the same time, for example downloading the file while listening to music and concurrently playing the game. Thread concept is used to get a multithreading concept, first, we need to understand what is a thread?<br \/>\nMultitasking can be either <strong>process-based<\/strong> or <strong>thread-based<\/strong>.<br \/>\nIn Process-Based multitasking, multiple threads are executing simultaneously on the same OS. Example: Downloading, listening to songs, and playing a game.<br \/>\nThread Based multitasking is a single process that consists of separate tasks. Example: A game of FIFA consists of various threads.<\/p>\n<p><strong>Thread<\/strong> A smallest unit of multithreading is a thread. Threads archives separate tasks for the same process. It\u2019s an independent flow of execution. A single process consists of multiple threads. Each thread in a program performs a particular task. For example, A particular game is a single process that consists of several threads like playing musing, takes input from the user, running components, etc.<\/p>\n<h2>Multithreading in python can be in two scenarios:<\/h2>\n<ul>\n<li>Multiple tasks need to achieved<\/li>\n<li>A task does not have any interdependency<\/li>\n<\/ul>\n<p><strong>In python to use the multithreading have to import the threading module<\/strong><br \/>\n<em>import threading<br \/>\nfrom threading import *<\/em><br \/>\nTo import this module you have to install it on the anaconda platform by using :<br \/>\n<code>Conda install \u2013c conda-forge tbb<\/code>\t<\/p>\n<h3>How to create a thread in python<\/h3>\n<ul>\n<li>Without creating class<\/li>\n<li>By Extending Thread Class<\/li>\n<li>Without extending Thread class<\/li>\n<\/ul>\n<h3>Without Creating a class<\/h3>\n<p>Every process has one thread that is always executing which is a \u2018main\u2019 thread. Defining a thread using the \u2018Thread\u2019 class that exists in the threading module is explained below.<br \/>\n<code>from threading import *<br \/>\ndef my_thread():<br \/>\n\tfor x in range(6):<br \/>\n\t\tprint(\u201cmy thread\u201d)<br \/>\nt1=Thread(target= my_thread)<br \/>\nt1.start()<br \/>\nprint(\u201cfinished\u201d)<\/code><\/p>\n<p>t1=Thread(target= my_thread) creates the new thread and specifies the target of this child thread that is the \u2018my_thread\u2019 function and t1.start() starts the execution of the child thread t1.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/02\/python1.png\" alt=\"multithreading python1\" \/><\/p>\n<p>As we can see the final statement is printed with the help of the \u2018main\u2019 thread without waiting for the completion of the execution of the child thread that is my_thread. If the user wants the main thread to wait for the child thread to finish, we can use t1.join(). We can use the current_thread.getName() method to see which statement is called by the child thread and the main thread. \u2018main\u2019 thread is a thread that creates the child thread as in the program we print the current thread before initiating the child thread.<br \/>\n<code>from threading import *<br \/>\ndef my_thread():<br \/>\n\tfor x in range(6):<br \/>\n\t\tprint(\"my thread..\",current_thread().getName())<br \/>\nprint(current_thread().getName())<br \/>\nt1=Thread(target= my_thread)<br \/>\nt1.start()<br \/>\nt1.join()<br \/>\nprint(\"finished\", current_thread().getName())<br \/>\n<\/code><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/02\/python2.png\" alt=\"multithreading python2\"\/><\/p>\n<p>In the output, it is very clear that the main thread lies all the control before the process begins and Thread28 is the child thread also the final statement is executed by the main thread.<\/p>\n<h3>By Extending Thread Class<\/h3>\n<p>When you extend the thread class to create a thread you can overwrite only two functions that are run() and init() and for every python function that is defined within the class, the self parameter has to be specified.<\/p>\n<p><code>import threading<br \/>\nfrom threading import *<br \/>\nclass A(threading.Thread):<br \/>\n    def run(self):<br \/>\n        for x in range(6):<br \/>\n            print(\"child thread..\",current_thread().getName())<br \/>\nobj=A()<br \/>\nobj.start()<br \/>\nobj.join()<br \/>\nprint(\"controlled return to \",current_thread().getName())<\/code><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/02\/python3.png\" alt=\"python3\" \/><\/p>\n<h3>Without Extending the Thread Class<\/h3>\n<p><code>from threading import *<br \/>\nclass A:<br \/>\n    def my_thread(self):<br \/>\n        lst=[2,1,'w',66,'g','abc']\n        for x in lst:<br \/>\n            print(\"child printing\",x)<br \/>\nmy_obj=A()<br \/>\nt1=Thread(target=my_obj.my_thread())<br \/>\nt1.start()<br \/>\nt1.join()<br \/>\nprint(\"done\")<\/code><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/02\/python4.png\" alt=\"python4\"\/><\/p>\n<h3>Advantages of Multithreading<\/h3>\n<ul>\n<li>Performance enhancement by decreasing the development time<\/li>\n<li>Simplified and streamlined program coding<\/li>\n<li>Allows simultaneous and parallelized execution of tasks<\/li>\n<li>Better use of CPU resources<\/li>\n<\/ul>\n<p><small><em>Get certification for your knowledge in the fundamentals of Computer functioning by clearing the <a href=\"https:\/\/www.studysection.com\/computer-foundation-diploma\">Computer Certification Exam<\/a> conducted by StudySection. After going through this Computer Certification exam, you will be able to evaluate your basic knowledge of computers.<\/em><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Multitasking refers to the ability of an operating system to perform different tasks at the same time, for example downloading<\/p>\n","protected":false},"author":1,"featured_media":3954,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[66,33],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>StudySection Blog - Multithreading in Python Programming Language<\/title>\n<meta name=\"description\" content=\"Multitasking refers to the ability of an operating system to perform different tasks at the same time, for exe download the music file.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/multithreading-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"StudySection Blog - Multithreading in Python Programming Language\" \/>\n<meta property=\"og:description\" content=\"Multitasking refers to the ability of an operating system to perform different tasks at the same time, for exe download the music file.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/multithreading-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2021-02-03T04:21:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-02-03T06:24:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/02\/python.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\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\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/multithreading-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/multithreading-in-python\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Multithreading in Python\",\"datePublished\":\"2021-02-03T04:21:28+00:00\",\"dateModified\":\"2021-02-03T06:24:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/multithreading-in-python\/\"},\"wordCount\":550,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"programming\",\"Python\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/multithreading-in-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/multithreading-in-python\/\",\"url\":\"https:\/\/studysection.com\/blog\/multithreading-in-python\/\",\"name\":\"StudySection Blog - Multithreading in Python Programming Language\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2021-02-03T04:21:28+00:00\",\"dateModified\":\"2021-02-03T06:24:14+00:00\",\"description\":\"Multitasking refers to the ability of an operating system to perform different tasks at the same time, for exe download the music file.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/multithreading-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/multithreading-in-python\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/multithreading-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Multithreading in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"StudySection Blog - Multithreading in Python Programming Language","description":"Multitasking refers to the ability of an operating system to perform different tasks at the same time, for exe download the music file.","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:\/\/studysection.com\/blog\/multithreading-in-python\/","og_locale":"en_US","og_type":"article","og_title":"StudySection Blog - Multithreading in Python Programming Language","og_description":"Multitasking refers to the ability of an operating system to perform different tasks at the same time, for exe download the music file.","og_url":"https:\/\/studysection.com\/blog\/multithreading-in-python\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2021-02-03T04:21:28+00:00","article_modified_time":"2021-02-03T06:24:14+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/02\/python.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/multithreading-in-python\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/multithreading-in-python\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Multithreading in Python","datePublished":"2021-02-03T04:21:28+00:00","dateModified":"2021-02-03T06:24:14+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/multithreading-in-python\/"},"wordCount":550,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["programming","Python"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/multithreading-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/multithreading-in-python\/","url":"https:\/\/studysection.com\/blog\/multithreading-in-python\/","name":"StudySection Blog - Multithreading in Python Programming Language","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2021-02-03T04:21:28+00:00","dateModified":"2021-02-03T06:24:14+00:00","description":"Multitasking refers to the ability of an operating system to perform different tasks at the same time, for exe download the music file.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/multithreading-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/multithreading-in-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/multithreading-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Multithreading in Python"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":352,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3951"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=3951"}],"version-history":[{"count":3,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3951\/revisions"}],"predecessor-version":[{"id":3960,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3951\/revisions\/3960"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/3954"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=3951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=3951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=3951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}