PYTHON Thread based parallism

PYTHON Thread based parallism

Python Thread-based Parallelism

A multi-threaded program consists of sub-programs each of which
is handled separately by different threads. Multi-threading allows for
parallelism in program execution. All the active threads run concurrently,
sharing the CPU resources effectively and thereby, making the program execution
faster. Multi-threading is generally used when:

1.  There are sub-programs whose output needs to be combined by the
main program.

2.  The main program contains sections of code that are relatively
independent of each other

*A multi-threaded program handles different tasks at the same
time, within the same process, where different threads share the data space
with each other as well as the main thread.

Starting a new thread

The threading module in python provides function calls that is used to create new threads. The   __init__ function is used for initializing the data associated with the new threads whereas, the run function defines the threads behavior as soon as the thread starts its execution.
In order to create a new thread:-

1.    Create a sub class of the thread class.

2.    Override the __init__ function of the thread class. This method will initialize the date specific to a thread.

            3.   Override the run method to define the behavior of the thread.

Python program to initialize a new thread

import threading

class thread(threading.Thread):
def __init__(self, thread_name, thread_ID):
self.thread_name = thread_name
self.thread_ID = thread_ID
def run(self):
print(str(self.thread_name) +" "+ str(self.thread_ID));

thread1 = thread("GFG", 1000)
thread2 = thread("GeeksforGeeks", 2000);




Threading module

Python provides Threading module which is very powerful and also provides high level support for threads. Threading module defines lots of functions which are used to obtain thread related data and these functions are executed automatically.


This function returns the number of Thread objects currently alive. Here the returned count is equal to the length of the list returned by enumerate().


This function returns the current Thread object and it is corresponding to the caller%u2019s thread of control.


This function returns the %u2018thread identifier%u2019 of the current thread. This is a nonzero integer.


This function returns a list of all Thread objects currently alive includes daemonic threads, current_thread() function creates a dummy thread and the main thread and excludes terminated threads and threads that have not yet been started.


This function returns the main Thread object.


When all threads are started from the threading module, set a trace function. Before run() method is called, this function is passed to sys.settrace() for each thread.


When all threads are started from the threading module, set a profile function. Before run() method is called, this function is passed to sys.setprofile() for each thread.


This function returns the size of the thread stack and it is used when creating new threads.

  *Program consist of all these module

import threading

def trace_function():
print("Passing the trace function")
def profile():
print("Setting the profile of thread: " + str(threading.current_thread().getName()))

class thread(threading.Thread):
def __init__(self, thread_name, thread_ID):
self.thread_name = thread_name
self.thread_ID = thread_ID
def run(self):
print("Number of active threads: "+ str(threading.active_count()))
print("Name of current thread: " + str(threading.current_thread().getName()))

thread1 = thread("CPP", 1000)
thread2 = thread("CPP secrets", 2000);
print("Name of main thread: " + str(threading.main_thread().getName()))
print("Identity of main thread: "+ str(threading.get_ident()))
print("Stack size = " + str(threading.stack_size()))

print("Enumeration list: ")


Name of main thread: MainThread

Identity of main thread: 428
Stack size = 0
Passing the trace function
Setting the profile of thread: MainThread
Number of active threads: 6
Name of current thread: Thread-132000
Number of active threads: 7
Name of current thread: Thread-14Enumeration list:

*In the next article we will learn about threading.active_count()

Link :-  javascript:nicTemp();

More Articles of Diksha Kumari:

Name Views Likes
Python : py_compile | Generate byte code 308 0
Python : py_compiler |Convert py to pyc 1157 0
Python : py_compile |Compile python sources file 287 0
Python :How to lock Critical Sections(Thread based parallelism) 249 0
Python : Locking without Deadlocks(Thread based Parallelism ) 180 0
Python : Communicating Between Threads-2(Thread based parallelism) 389 0
Python : Communicating Between Threads -1(Thread based parallelism) 176 0
Python Start and Stop a thread (Thread based parallelism) 165 0
Python: Check if a Thread has started (thread based parallelism) 144 0
Python Thread-based parallelism :main_thread() 229 0
Python Lock Class | acquire() Method(thread based parallelism) 294 0
Python Thread-based parallelism : get_native_id() 460 0
Python Thread-based parellelism: active_count() 269 1
Python Lock Class | lock() Method(thread based parallelism) 246 0
Python Thread-based parallelism : stack_size([size]) 331 1
Python Thread Local Data(thread based parallelism 265 1
Python Semaphore Objects (thread based parallelism) 264 0
Python Event class - set() method | Thread based parallelism 252 1
Python Condition object (thread based parallelism) 411 0
Python Thread based parallelism : Run() 228 0
Python Thread-based parallelism : current_thread() 254 1
Python Condition object - acquire()(thread based parallelism) 190 0
Python Thread-based parallelism : TIMEOUT_MAX () 423 0
Python Condition object - notify_all() ( thread based parallelism ) 192 0
Python Thread-based parallelism :.settrace(func) 308 0
Python difference lock and RLock object : Thread based parallelism 1082 0
Python Event class - clear() method | Thread based parallelism 188 0
Python Thread object -start() (thread based parallelism) 335 0
Python RLock Class - acquire() Method (thread based parallelism) 226 0
Python Thread-based parallelism : enumerate() 246 0
Python Thread based parallelism: get_ident() 302 1
Python Thread-based parallelism : setprofile(func) 215 0
Python RLock Class - release() Method (thread based parallelism) 194 0
Python Barrier object | Thread based parallelism 228 0
Python Condition object - notify () ( thread based parallelism ) 217 0
Python Event class - is_set() method | Thread based parallelism 173 0
Python Thread based parallelism : join() 317 0
Python thread based parallelism : 200 0
Python Event class - wait() method | Thread based parallelism 243 0
PYTHON Thread based parallism 293 1
Python Barrier method | Thread based parallelism 228 0
Python Condition object - release() ( thread based parallelism ) 184 0
Python Thread based parallelism: excepthook 1487 1
Python thread object : is_alive() (thread based parallelism) 996 0
Python Condition object - wait() ( thread based parallelism ) 215 0
Python Lock Class | release() Method(thread based parallelism) 254 0
Python Thread object (thread based parallelism) 259 0
Python Timer class - start() method | Thread based parallelism 175 1
Python thread based parallelism : 322 1
Python Timer class - cancel() method | Thread based parallelism 206 1
Python thread object : Deamon() (thread based parallelism) 241 0


  • sanjot
    25-May-2021 10:11:16 PM
    This article was very useful.Thank you Diksha.