How to Create Thread without Inheritance in Python

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

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.

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.

Topic Explanation:

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.

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.

Prerequisite:

Proficiency in Python Basics.
Familiarity with Functions.
Conceptual Understanding of Multithreading.
Basic Knowledge of Python Modules.
Awareness of Synchronization Concepts.

Code With Comments:

# Importing necessary libraries
import time
from threading import Thread

# Defining a function 'display' to print a string and numbers from 1 to 10 with a delay of 1 second
def display(str):
    for i in range(1, 11):
        print(str, end=" ")
        print(i)
        time.sleep(1)

# Defining a function 'show' to print a string and numbers from 100 to 110 with a delay of 1 second
def show(str):
    for i in range(100, 111):
        print(str, end=" ")
        print(i)
        time.sleep(1)

# Defining a function 'abc' to print a string and numbers from 100 to 110 with a delay of 1 second
def abc(str):
    for i in range(100, 111):
        print(str, end=" ")
        print(i)
        time.sleep(1)

# Creating threads with target functions and passing arguments
T1 = Thread(target=display, args=("First",))
T2 = Thread(target=show, args=("Second",))
T3 = Thread(target=abc, args=("Third",))

# Starting the threads
T1.start()
T2.start()
T3.start()

Output:

First 1
Second 100
Third 100
First 2
Second 101
Third 101
First 3
Second 102
Third 102

Code Explanation:

Explanation:

  • import time: Imports the time module for introducing delays in the program.
  • from threading import Thread: Imports the Thread class from the threading module to facilitate multithreading.

Functions:

  • display(str): Prints a string and numbers from 1 to 10 with a 1-second delay.
  • show(str): Prints a string and numbers from 100 to 110 with a 1-second delay.
  • abc(str): Prints a string and numbers from 100 to 110 with a 1-second delay.

Thread Creation and Invocation:

  • T1 = Thread(target=display, args=(“First”,)): Creates a thread (T1) with the target function display and passes the argument “First.”
  • T2 = Thread(target=show, args=(“Second”,)): Creates a thread (T2) with the target function show and passes the argument “Second.”
  • T3 = Thread(target=abc, args=(“Third”,)): Creates a thread (T3) with the target function abc and passes the argument “Third.”
  • T1.start(), T2.start(), T3.start(): Initiates the execution of the threads.

Conclusion:

Wrapping up our venture into Python threading, we’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’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!

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

courses

DataFlair Team

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.

Leave a Reply

Your email address will not be published. Required fields are marked *