Javatpoint Logo
Javatpoint Logo

Thread-based Parallelism in Python

A thread is a group of commands in computer science that can be controlled separately by a scheduler, a part of the operating system.

The thread is used to run multiple threads simultaneously. A thread means multiple tasks and function calls in the program. A multi-threaded program has sub-programs that are managed by an individual thread. Parallelism in software execution is made possible via multi-threading. The active threads run simultaneously and share the CPU resources resulting in fast execution of the program.

Uses of Multi-threading

  • Multi-threading is used when the main program must integrate the outputs of the sub-programs.
  • Multi-threading is used when the main program has independent codes.

Threading Module in Python

Python provides a powerful module named threading that supports different threads.

It gives various functions for obtaining the threads and their related data. Moreover, these functions are automatically executed. These functions and methods are

  1. threading.active_count(): This function returns the active threads count currently running. The count returned is the length of the list which is returned by the enumerate() function.
  2. threading.get_ident(): This function is used as the thread identifier of the running thread. It is a non-zero integer.
  3. threading.enumerate(): The enumerate function returns a list of all the active threads running currently, including the daemonic threads.
  4. threading.current_thread: This function gives the current thread object, which corresponds to the caller's thread of object. A dummy thread object with limited functionality is returned if the caller's thread of control was not generated using the threading module.
  5. threading.main_thread(): It returns the main thread object.
  6. threading.setprofile(func): It is used when any thread is started from the module. This function is passed to sys.setprofile() for every thread before execution of the run() method.
  7. threading.stack_size([size]): It returns the thread stack size. It is used at the time of creating new threads.
  8. threading.settrace(func): This function is used when any thread is started from the module. This function is passed to sys.settrace() for every thread before execution of the run() method.
  9. threading.TIMEOUT_MAX: This is a constant having the maximum value, which is used for the timeout parameter of blocking functions (Lock.acquire(), RLock.acquire(), Condition.wait(), etc.)

Importing Threading Module in Python

Let's implement the threading concept in Python.

Creating a new thread

The threading module is used to create new threads. The data in the new thread created is initialized with the help of the __init__ function. The run function defines the thread's behavior after the execution of the new thread starts.

We must follow these steps to create a new thread:

  1. We must create a sub-class of the thread class.
  2. Now, override the __init__ function of the thread class. It will initialize the data of the thread.
  3. Then, override the run method, which helps to define the thread behavior.

Program 1: Program to create a new thread in Python.

Code:

Output:

Creating new threads
JTP  2300
Javatpoint  5000
Exit

Explanation:

In this, firstly, we have imported the threading module. Then, we made a class named new_thread, and then, using the __init__ function, we initialized the threading class. Then, using the run() function, we defined the thread function. Then we made two thread objects with their IDs. Then, we will start the thread creation; it will print the thread name and ID.

Program 2: Program to illustrate the thread and its functions in Python.

Code:

Output:

Name of  the main thread : MainThread
Identity of the main thread : 10528
Stack size is : 0
Passing the trace function
None
Set the profile of thread: MainThread
1500
Total Number of active threads: 7
Name of the current thread: Thread-19
2900
Total Number of active threads: 7
Name of the current thread: Thread-20
Enumeration list : 
[<_MainThread(MainThread, started 10528)>, , , , , ]
Exit

Explanation:

We first imported the threading module. Then, we called two functions for the trace function and set the profile of the threads. Then we made a thread class then initialized the data of the thread. Then we have to print the details of the thread, like its name, identity, and enumeration list.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news