{"id":12333,"date":"2018-12-02T18:14:13","date_gmt":"2018-12-02T12:44:13","guid":{"rendered":"http:\/\/pratikkataria.com\/blog\/?p=12333"},"modified":"2018-12-02T18:36:24","modified_gmt":"2018-12-02T13:06:24","slug":"defining-a-thread-in-python","status":"publish","type":"post","link":"https:\/\/pratikkataria.com\/blog\/defining-a-thread-in-python\/","title":{"rendered":"Defining a Thread in Python"},"content":{"rendered":"<div class=\"fusion-fullwidth fullwidth-box fusion-builder-row-1 nonhundred-percent-fullwidth non-hundred-percent-height-scrolling\" style=\"background-color: #ffffff;background-position: center center;background-repeat: no-repeat;padding-top:10px;padding-right:10px;padding-bottom:10px;padding-left:10px;margin-bottom: 0px;margin-top: 0px;border-width: 0px 0px 0px 0px;border-color:#eae9e9;border-style:solid;\" ><div class=\"fusion-builder-row fusion-row\"><div class=\"fusion-layout-column fusion_builder_column fusion-builder-column-0 fusion_builder_column_1_1 1_1 fusion-one-full fusion-column-first fusion-column-last\" style=\"margin-top:0px;margin-bottom:20px;\"><div class=\"fusion-column-wrapper fusion-flex-column-wrapper-legacy\" style=\"background-position:left top;background-repeat:no-repeat;-webkit-background-size:cover;-moz-background-size:cover;-o-background-size:cover;background-size:cover;padding: 0px 0px 0px 0px;\"><div class=\"fusion-text fusion-text-1\"><h2>The Python Threading Module<\/h2>\n<ul>\n<li>Python manages a thread via the threading package<\/li>\n<li>It provides some very interesting features that make the threading-based approach easier<\/li>\n<li>The threading module provides several synchronization mechanisms that are very simple to implement<\/li>\n<\/ul>\n<h2>Major Components of Threading Module<\/h2>\n<ul>\n<li>thread object<\/li>\n<li>lock object<\/li>\n<li>RLock object<\/li>\n<li>semaphore object<\/li>\n<li>condition object<\/li>\n<li>event object<\/li>\n<\/ul>\n<h2>Defining a Thread<\/h2>\n<ul>\n<li>The simplest way to use a thread is to instantiate it with a target function and then call the start() method<\/li>\n<li>The python module threading has the Thread() method<\/li>\n<\/ul>\n<h2>Syntax to Define a Thread<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">class threading.Thread(group=None,\r\ntarget=None,\r\nname=None,\r\nargs=(),\r\nkwargs={})<\/pre>\n<ul>\n<li>target is the function to be executed when we start the thread activity<\/li>\n<li>name is name of the thread. By default a unique thread name as threadN is assigned to it.<\/li>\n<li>args is the tuple of arguments that is to be passed to the target function<\/li>\n<li>kwargs is the dictionary of keyword arguments that are to be used by target function<\/li>\n<li>It&#8217;s useful to pass arguments that tell a thread what work to do.<\/li>\n<\/ul>\n<h2>Sample Code<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">import threading\r\n\r\n\r\ndef func(i):\r\n    print(\"Function called by thread {} \\n\".format(i))\r\n    return\r\n\r\n\r\nthreads = []\r\n\r\nfor i in range(5):\r\n    t = threading.Thread\\\r\n        (target=func, args=(i,))\r\n    threads.append(t)\r\n    t.start()\r\n    t.join()\r\n<\/pre>\n<ul>\n<li>thread doesn&#8217;t starts running until start() method is called.<\/li>\n<li>join()\u00a0 makes the calling thread wait until the thread has finished the execution<\/li>\n<li><em>NOTE: the output order cannot be predetermined as multiple thread might print output back to STD out at the same time.<\/em><\/li>\n<\/ul>\n<h2>Determining the Current Thread<\/h2>\n<ul>\n<li>Instead of passing an argument to determine current thread, we can use:\u00a0 threading.current_thread().getName()<\/li>\n<li>The name of thread can be changed with the name argument: threading.Thread(name=&#8217;func_thread&#8217;, target=func)<\/li>\n<\/ul>\n<\/div><div class=\"fusion-clearfix\"><\/div><\/div><\/div><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":12335,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[56,212,38],"tags":[283,218,213,217],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v15.6.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Defining a Thread in Python - TechAmass<\/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:\/\/pratikkataria.com\/blog\/defining-a-thread-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Defining a Thread in Python - TechAmass\" \/>\n<meta property=\"og:url\" content=\"https:\/\/pratikkataria.com\/blog\/defining-a-thread-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"TechAmass\" \/>\n<meta property=\"article:published_time\" content=\"2018-12-02T12:44:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-12-02T13:06:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/pratikkataria.com\/blog\/wp-content\/uploads\/2018\/12\/python-coding-programming.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1600\" \/>\n\t<meta property=\"og:image:height\" content=\"1067\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<meta name=\"twitter:creator\" content=\"@PratikPKataria\" \/>\n<meta name=\"twitter:site\" content=\"@PratikPKataria\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"3 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/pratikkataria.com\/blog\/#website\",\"url\":\"https:\/\/pratikkataria.com\/blog\/\",\"name\":\"TechAmass\",\"description\":\"Pratik&#039;s blog\",\"publisher\":{\"@id\":\"https:\/\/pratikkataria.com\/blog\/#\/schema\/person\/ef09a6ee5cb751524bc4c92454c59412\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/pratikkataria.com\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/pratikkataria.com\/blog\/defining-a-thread-in-python\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/pratikkataria.com\/blog\/wp-content\/uploads\/2018\/12\/python-coding-programming.jpg\",\"width\":1600,\"height\":1067},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/pratikkataria.com\/blog\/defining-a-thread-in-python\/#webpage\",\"url\":\"https:\/\/pratikkataria.com\/blog\/defining-a-thread-in-python\/\",\"name\":\"Defining a Thread in Python - TechAmass\",\"isPartOf\":{\"@id\":\"https:\/\/pratikkataria.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/pratikkataria.com\/blog\/defining-a-thread-in-python\/#primaryimage\"},\"datePublished\":\"2018-12-02T12:44:13+00:00\",\"dateModified\":\"2018-12-02T13:06:24+00:00\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/pratikkataria.com\/blog\/defining-a-thread-in-python\/\"]}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/pratikkataria.com\/blog\/defining-a-thread-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/pratikkataria.com\/blog\/defining-a-thread-in-python\/#webpage\"},\"author\":{\"@id\":\"https:\/\/pratikkataria.com\/blog\/#\/schema\/person\/ef09a6ee5cb751524bc4c92454c59412\"},\"headline\":\"Defining a Thread in Python\",\"datePublished\":\"2018-12-02T12:44:13+00:00\",\"dateModified\":\"2018-12-02T13:06:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/pratikkataria.com\/blog\/defining-a-thread-in-python\/#webpage\"},\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/pratikkataria.com\/blog\/#\/schema\/person\/ef09a6ee5cb751524bc4c92454c59412\"},\"image\":{\"@id\":\"https:\/\/pratikkataria.com\/blog\/defining-a-thread-in-python\/#primaryimage\"},\"keywords\":\"code,parallel programming,python,threads\",\"articleSection\":\"Engineering,Python,Tech\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/pratikkataria.com\/blog\/defining-a-thread-in-python\/#respond\"]}]},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/pratikkataria.com\/blog\/#\/schema\/person\/ef09a6ee5cb751524bc4c92454c59412\",\"name\":\"Pratik Kataria\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/pratikkataria.com\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/pratikkataria.com\/blog\/wp-content\/uploads\/2020\/02\/kpratik_sq.jpg\",\"width\":512,\"height\":512,\"caption\":\"Pratik Kataria\"},\"logo\":{\"@id\":\"https:\/\/pratikkataria.com\/blog\/#personlogo\"},\"description\":\"Pratik Kataria is currently learning Springboot and Hibernate. Technologies known and worked on: C\/C++, Java, Python, JavaScript, HTML, CSS, Wordpress, Angular, Ionic, MongoDB, SQL and Android. Softwares known and worked on: Adobe Photoshop, Adobe Illustrator and Adobe After Effects.\",\"sameAs\":[\"https:\/\/pratikkataria.com\/\"]}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","amp_enabled":true,"acf":[],"_links":{"self":[{"href":"https:\/\/pratikkataria.com\/blog\/wp-json\/wp\/v2\/posts\/12333"}],"collection":[{"href":"https:\/\/pratikkataria.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pratikkataria.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pratikkataria.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pratikkataria.com\/blog\/wp-json\/wp\/v2\/comments?post=12333"}],"version-history":[{"count":3,"href":"https:\/\/pratikkataria.com\/blog\/wp-json\/wp\/v2\/posts\/12333\/revisions"}],"predecessor-version":[{"id":12338,"href":"https:\/\/pratikkataria.com\/blog\/wp-json\/wp\/v2\/posts\/12333\/revisions\/12338"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pratikkataria.com\/blog\/wp-json\/wp\/v2\/media\/12335"}],"wp:attachment":[{"href":"https:\/\/pratikkataria.com\/blog\/wp-json\/wp\/v2\/media?parent=12333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pratikkataria.com\/blog\/wp-json\/wp\/v2\/categories?post=12333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pratikkataria.com\/blog\/wp-json\/wp\/v2\/tags?post=12333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}