

{"id":125979,"date":"2023-11-15T19:02:25","date_gmt":"2023-11-15T13:32:25","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=125979"},"modified":"2024-02-29T14:47:30","modified_gmt":"2024-02-29T09:17:30","slug":"how-to-create-thread-without-inheritance-in-python","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/how-to-create-thread-without-inheritance-in-python\/","title":{"rendered":"How to Create Thread without Inheritance in Python"},"content":{"rendered":"<p>In the realm of Python programming, threading emerges as a powerful technique to execute multiple tasks concurrently, enhancing the efficiency of your programs. Today, we embark on a practical journey to explore the creation of threads without relying on inheritance.<\/p>\n<p>By specifically delving into the approach of using a target function, we aim to simplify the intricacies associated with threading, making it more accessible for developers seeking effective multitasking solutions.<\/p>\n<h2>Topic Explanation:<\/h2>\n<p>Creating threads without inheritance in Python involves leveraging the concept of a target function. Unlike the traditional approach of inheriting from the Thread class, this method revolves around defining a function and designating it as the target for your thread. The steps include importing the threading module, creating a thread object with the specified target function, and then initiating the thread using the start() method.<\/p>\n<p>This pragmatic approach not only streamlines the process but also provides a clearer and more modular structure to your multithreading endeavors. By comprehending and implementing this technique, developers can seamlessly integrate multithreading into their Python programs, fostering responsiveness and efficiency without delving into the complexities of class inheritance.<\/p>\n<h3>Prerequisite:<\/h3>\n<p>Proficiency in Python Basics.<br \/>\nFamiliarity with Functions.<br \/>\nConceptual Understanding of Multithreading.<br \/>\nBasic Knowledge of Python Modules.<br \/>\nAwareness of Synchronization Concepts.<\/p>\n<h3>Code With Comments:<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># Importing necessary libraries\r\nimport time\r\nfrom threading import Thread\r\n\r\n# Defining a function 'display' to print a string and numbers from 1 to 10 with a delay of 1 second\r\ndef display(str):\r\n    for i in range(1, 11):\r\n        print(str, end=\" \")\r\n        print(i)\r\n        time.sleep(1)\r\n\r\n# Defining a function 'show' to print a string and numbers from 100 to 110 with a delay of 1 second\r\ndef show(str):\r\n    for i in range(100, 111):\r\n        print(str, end=\" \")\r\n        print(i)\r\n        time.sleep(1)\r\n\r\n# Defining a function 'abc' to print a string and numbers from 100 to 110 with a delay of 1 second\r\ndef abc(str):\r\n    for i in range(100, 111):\r\n        print(str, end=\" \")\r\n        print(i)\r\n        time.sleep(1)\r\n\r\n# Creating threads with target functions and passing arguments\r\nT1 = Thread(target=display, args=(\"First\",))\r\nT2 = Thread(target=show, args=(\"Second\",))\r\nT3 = Thread(target=abc, args=(\"Third\",))\r\n\r\n# Starting the threads\r\nT1.start()\r\nT2.start()\r\nT3.start()<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>First 1<br \/>\nSecond 100<br \/>\nThird 100<br \/>\nFirst 2<br \/>\nSecond 101<br \/>\nThird 101<br \/>\nFirst 3<br \/>\nSecond 102<br \/>\nThird 102<br \/>\n&#8230;<\/p>\n<h3>Code Explanation:<\/h3>\n<h4>Explanation:<\/h4>\n<ul>\n<li><strong>import time:<\/strong> Imports the time module for introducing delays in the program.<\/li>\n<li><strong>from threading import Thread:<\/strong> Imports the Thread class from the threading module to facilitate multithreading.<\/li>\n<\/ul>\n<h4>Functions:<\/h4>\n<ul>\n<li><strong>display(str):<\/strong> Prints a string and numbers from 1 to 10 with a 1-second delay.<\/li>\n<li><strong>show(str):<\/strong> Prints a string and numbers from 100 to 110 with a 1-second delay.<\/li>\n<li><strong>abc(str):<\/strong> Prints a string and numbers from 100 to 110 with a 1-second delay.<\/li>\n<\/ul>\n<h4>Thread Creation and Invocation:<\/h4>\n<ul>\n<li><strong>T1 = Thread(target=display, args=(&#8220;First&#8221;,)):<\/strong> Creates a thread (T1) with the target function display and passes the argument &#8220;First.&#8221;<\/li>\n<li><strong>T2 = Thread(target=show, args=(&#8220;Second&#8221;,)):<\/strong> Creates a thread (T2) with the target function show and passes the argument &#8220;Second.&#8221;<\/li>\n<li><strong>T3 = Thread(target=abc, args=(&#8220;Third&#8221;,)):<\/strong> Creates a thread (T3) with the target function abc and passes the argument &#8220;Third.&#8221;<\/li>\n<li><strong>T1.start(), T2.start(), T3.start():<\/strong> Initiates the execution of the threads.<\/li>\n<\/ul>\n<h3>Conclusion:<\/h3>\n<p>Wrapping up our venture into Python threading, we&#8217;ve walked through a hands-on expedition, emphasizing the formation of threads without the conventional use of inheritance. Through the utilization of a targeted function method, we&#8217;ve unraveled the intricacies surrounding threading, striving to provide an easier route for developers aiming to tap into the potential of simultaneous multitasking. As you incorporate these methods into your Python coding toolkit, the efficiency enhancements derived from threading will play a pivotal role in shaping agile and dynamic applications. Enjoy your coding endeavors!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the realm of Python programming, threading emerges as a powerful technique to execute multiple tasks concurrently, enhancing the efficiency of your programs. Today, we embark on a practical journey to explore the creation&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[28947,28945,28946,10333,28626],"class_list":["post-125979","post","type-post","status-publish","format-standard","hentry","category-python","tag-create-thread-using-target","tag-how-to-create-thread-without-inheritance","tag-how-to-create-thread-without-inheritance-in-python","tag-python","tag-python-practical"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Create Thread without Inheritance in Python - DataFlair<\/title>\n<meta name=\"description\" content=\"Creating threads without inheritance in Python involves leveraging the concept of a target function.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/how-to-create-thread-without-inheritance-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Create Thread without Inheritance in Python - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Creating threads without inheritance in Python involves leveraging the concept of a target function.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/how-to-create-thread-without-inheritance-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-15T13:32:25+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-29T09:17:30+00:00\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Create Thread without Inheritance in Python - DataFlair","description":"Creating threads without inheritance in Python involves leveraging the concept of a target function.","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:\/\/data-flair.training\/blogs\/how-to-create-thread-without-inheritance-in-python\/","og_locale":"en_US","og_type":"article","og_title":"How to Create Thread without Inheritance in Python - DataFlair","og_description":"Creating threads without inheritance in Python involves leveraging the concept of a target function.","og_url":"https:\/\/data-flair.training\/blogs\/how-to-create-thread-without-inheritance-in-python\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-11-15T13:32:25+00:00","article_modified_time":"2024-02-29T09:17:30+00:00","author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/how-to-create-thread-without-inheritance-in-python\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/how-to-create-thread-without-inheritance-in-python\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"How to Create Thread without Inheritance in Python","datePublished":"2023-11-15T13:32:25+00:00","dateModified":"2024-02-29T09:17:30+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/how-to-create-thread-without-inheritance-in-python\/"},"wordCount":462,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"keywords":["create thread using target","How to Create Thread without Inheritance","How to Create Thread without Inheritance in python","Python","python practical"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/how-to-create-thread-without-inheritance-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/how-to-create-thread-without-inheritance-in-python\/","url":"https:\/\/data-flair.training\/blogs\/how-to-create-thread-without-inheritance-in-python\/","name":"How to Create Thread without Inheritance in Python - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"datePublished":"2023-11-15T13:32:25+00:00","dateModified":"2024-02-29T09:17:30+00:00","description":"Creating threads without inheritance in Python involves leveraging the concept of a target function.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/how-to-create-thread-without-inheritance-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/how-to-create-thread-without-inheritance-in-python\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/how-to-create-thread-without-inheritance-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"How to Create Thread without Inheritance in Python"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam6\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/125979","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=125979"}],"version-history":[{"count":4,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/125979\/revisions"}],"predecessor-version":[{"id":134289,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/125979\/revisions\/134289"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=125979"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=125979"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=125979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}