Python multithreading example

Python multithreading example: Thread is an entity that performs the schedule for execution and the smallest unit of processing. The execution is performed over an operating system. The thread is a sequence of instructions within the program and executed independently. Any process has 3 basic components:

  1. The executable program.
  2. It is associated data needed by the program.
  3. The execution of the context of the program

A thread has a starting point an execution sequence and the result also has an instruction pointer.
The Multiple thread process shares the same data space with the main thread and can therefore share information or communicate with each other more easily than if they were separate processes.
They are also called light-weight processes and do not require much memory overhead.

  1. Thread Identifier: The unique id is assigned to a new thread.
  2. Stack pointer: The points to thread stack in the process as stack contains the local variables under thread’s scope.
  3. Program counter: The register will store the address of the instruction currently being executed by thread.
  4. Thread state: It can be running, ready or waiting.
  5. Thread’s register set: registers assigned to thread for computations.

Multithreading:

Multithreading can execute multiple threads parallelly.
 Multithreading will improve the performance of any program and it is pretty user-friendly to learn quickly.

Threading Module:-

The module includes with Python 2.4 and provides powerful, high-level support for threads.
The threading module will expose all the methods of the thread module and provide methods as,

  1. threading.activeCount () – it return number of thread objects that are active.
  2. threading.currentThread () − Returns the number of thread objects in the caller thread control.
  3. threading.enumerate () − Returns a list of all active thread objects.

The methods will provide,

  1. run () − The run() method is the entry point for a thread.
  2. start () − The start() method starts a thread by calling the run method.
  3. join ([time]) − The join() waits for threads to terminate.
  4. isAlive() − The is Alive() method checks whether a thread is still executing.
  5. getName () − The getName() method returns the name of a thread.
  6. setName () − The setName() method sets the name of a thread.
Syntax:-
             thread.start_new_thread (function, args [, kwargs])

The method is efficient and straightforward for creating threads and used to run programs in both Linux and Windows.

Benefits of multithreading:-

  1. Multiple threads can be used to process and share the same data-space.
  2.  We can communicate with each other by sharing the information.
  3. They will use less memory overhead and lightweight processes.
  4. The program can remain responsive to input when the threads are used.
  5. Threads are used to share and process the global variable's memory.

Python multithreading Example:-

Import threading
Def print_cube (num):
“””
Function to print cube of given num
“””
Print (“Cube :{}”.format (num*num*num))
Def print_square (num):
“””
Function to print square of given num
“””
Print (“Square :{}”.format (num*num))
If__name__==”__main__”
T1=threading.Thread (target=print_square, args= (10,))
T1=threading.Thread (target=print_cube, args= (10,))
T1.start ()
T2.start ()
T1.join ()
T2.join ()
Print (“Done!”)
Output:-
Square: 100
Cube: 1000
Done!

Advantages Of Multithreading:-

  1. It will improve the speed of computation on multiprocessor or multi-core systems because each processor will handle a separate thread concurrently.
  2. Multithreading will allow a program to remain responsive while one thread waits for input, and another runs a GUI at the same time.
  3.  The threads of a process have access to its global variables if a global variable changes in one thread.

Disadvantages Of Multithreading:-

  1. The single processor system multithreading won’t hit the speed of computation.
  2. The performance may downgrade because of managing threads.
  3. The synchronization is used to prevent mutual exclusion while accessing shared resources as it leads to more memory and CPU utilization.
  4. It also increases the complexity of the program for making it difficult to debug.
Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India