Multithreading in python

So here we will start learning about multithreading in python.

First of all we need to learn why actually we need multithreading or multiprocessing.

what happens we write code run it and it shares a lot of common data with it is were getting processed one by one also some times what happens we need some process to be run in the background and another process in the foreground.

This is were we need to learn about multithreading. 

In multithreading we create a multiple threads of some part of main process which is being processed parallelly.

which drastically improves the performance of the program.

Now the main question how we achieve multithreading so that we can make out program much faster then ever.

For now I'm using python programming language to demonstrate the simple example and with another post we will learn deep about the concepts of mutithreading and multiprocessing about how we share the data and variable from and one thread to another thread , how we can make out programm safe from going in race which means changing the value using multiple thread at the same time which leads to not properly working of the threads.

 Here is a very simple piece of code.

import time

def sleep2():
    time.sleep(2) #this will make the program sleep for 2 seconds
    print("sleep 2 done")

def sleep1():
    time.sleep(2) #this will make the program sleep for 2 seconds
    print("sleep 1 done")

if __name__ == '__main__':
    sleep1()
    sleep2()
    print("Done")

Here in the above image we are seeing the program is taking 4 second to execute the task that is to sleep for 2 second in each function.

Now we will see the same using power of multithreadin in python.

import time
import threading

def sleep2():
    time.sleep(2) #this will make program sleep for 2 second
    print("sleep 2 done")

def sleep1():
    time.sleep(2) #this will make program sleep for 2 second
    print("\nsleep 1 done")

if __name__ == '__main__':
    t1=threading.Thread(target=sleep1)
    t2=threading.Thread(target=sleep2)
    t1.start() #for starting the thread
    t2.start()   
    t1.join() #for making main thread to wait until the child completes its process
    t2.join()
    print("Done")

Here we can clearly see the same program completed the task in 2 seconds which is just half the time it was taking while not using threads. which does made owr program 2 times faster

Here if we look closely to the code what we have done.

1. Firstly we have imported the threading module which provide the support to do multithreading in python.

    import threading

2. Second we have defined our fucntions

    sleep1() 

    sleep2()

3. Third we defined out threads

    t1=threading.Thread(target=sleep1)

    t2=threading.Thread(target=sleep2)

4.Forth we stated the thread using

    t1.start()

    t2.start()

5.We joined out all the thread so that the below code will until the main thread will to complete their process using

    t1.join()

    t2.join()

And then we have written out below code as usual.

We will learn about threading more in out upcoming blogs about how we can share the data between multiple threads.

For now thanks for reading.

Jai shree ram

Click to view my YouTube Channel

 Some Amazon Product link You may like Please Do check it out Once.

  

Comments