Thread-based parallelism in Python


A thread in computer science is a set of instructions that can be managed independently by a scheduler, which is a part of operating system.

The main function of Threading is to run multiple threads at a time. Threads means different tasks, function calls in the program and multiple threads run at the same time that does not means that they are executed on different machines.

Multi-threading is used in two cases.

  • When the outputs of the sub-programs need to combined with main program.

  • When main program contains piece of code that are relatively independent of each other.

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.

threading.active_count()

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().

threading.current_thread()

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

threading.get_ident()

This function returns the ‘thread identifier’ of the current thread. This is a nonzero integer.

threading.enumerate()

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.

threading.main_thread()

This function returns the main Thread object.

threading.settrace(func)

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.

threading.setprofile(func)

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.

threading.stack_size([size])

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

threading.TIMEOUT_MAX

This is a constant which has the maximum value allowed for the timeout parameter of blocking functions(Lock.acquire(), RLock.acquire(), Condition.wait(), etc.).

Example code

import threading
def trace_function():
   print("Passing the trace function")
   def profile():
      print("PROFILE THREAD: " + str(threading.current_thread().getName()))
      class mythread(threading.Thread):
      def __init__(self, thread_name, thread_ID):
         threading.Thread.__init__(self)
         self.thread_name = thread_name
         self.thread_ID = thread_ID
      def run(self):
         print(str(self.thread_ID));
         print("ACTIVE THREADS ARE: "+ str(threading.active_count()))
         print("CURRENT THREAD IS: " + str(threading.current_thread().getName()))
         my_thread1 = mythread("PP", 500)
         my_thread2 = mythread("PythonProgram", 1000);
         print("NAME OF THE MAIN THREAD: " + str(threading.main_thread().getName()))
         print("IDENTIFICATION OF MAIN THREAD: "+ str(threading.get_ident()))
         print("STACK SIZE = " + str(threading.stack_size()))
      print(threading.settrace(trace_function()))
   threading.setprofile(profile())
my_thread1.start()
my_thread2.start()
print("LIST OF ENUMERATION: ")
print(threading.enumerate())
print("EXIT")

Output

NAME OF THE MAIN THREAD: MainThread
IDENTIFICATION OF MAIN THREAD: 5436
STACK SIZE = 0
Passing the trace function
None
PROFILE THREAD: MainThread
500
1000LIST OF ENUMERATION: ACTIVE THREADS ARE: 6
[<_MainThread(MainThread, started 5436)>, <Thread(Thread-4, started daemon 1960)>, <Heartbeat(Thread-5, started daemon 6452)>, <HistorySavingThread(IPythonHistorySavingThread, started 4304)>, <mythread(Thread-8, started 8460)>, <mythread(Thread-9, started 4668)>]
EXIT

CURRENT THREAD IS: Thread-8

ACTIVE THREADS ARE: 5
CURRENT THREAD IS: Thread-9

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 26-Jun-2020

275 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements