Multi threading program in python

Multi threading program in python

Satya Python solution ,

Multi threading program in python: Multi-Thread is a multiple processes running concurrently in one CPU core processor. It is different then multiprocessing where one process run in different processor. In multi threading, threads share one CPU resources. This makes program run efficiently. Program runs in parallel making it faster, efficient and cost effective.

In this post, I will explain how we can write multi threading program in python.

Program without multi threading (Normal execution)

In the below code, you can see four different curl commands execute uDeploy REST API.  This returns response in Json format.

popen function execute all commands one after one. Therefore, it is slightly slow as the processor execute these command one after one. Command wait for previous command before execution of next.

import os

def task(cm):
    print os.popen(cm).read()
cmd="curl -s -k -u admin:***** 'https://satya-inspiron-5566:8445/cli/applicationProcess/info?application=MyApps&applicationProcess=APP_proc'"
cmd1="curl -s -k -u admin:***** 'https://satya-inspiron-5566:8445/cli/agentCLI/info?agent=myAgent'"
cmd2="curl -s -k -u admin:***** 'https://satya-inspiron-5566:8445/cli/applicationProcess/info?application=MyApps&applicationProcess=APP_proc'"
cmd3="curl -s -k -u admin:***** 'https://satya-inspiron-5566:8445/cli/agentCLI/info?agent=myAgent'"

task(cmd)
task(cmd1)
task(cmd2)
task(cmd3)

 

Script output

On execution it returns response one by one. As a result, execution is slow. In this,  processor wait for the previous process for completion before it execute next.

>time python normal_exec.py
........
........
real 0m0.506s
user 0m0.091s
sys 0m0.025s

Multi-threading in python by using “threading” module.

In this script I have used same code (as described above). However, in this code I have used threading module. All the command execute by Thread module.

In the below code, you can see task function defined as target. Second argument is tuple which is actually store variable defined with curl command. This act as arguments to the task function. Thread returns objects (T1, T2, etc). Using this Object we can start the thread by calling start() function.

Once the thread started, it execute task function with the argument. These arguments store uDeploy REST command.

import os
import threading

def task(cm):
print os.popen(cm).read()

cmd="curl -s -k -u admin:****** 'https://satya-inspiron-5566:8445/cli/applicationProcess/info?application=MyApps&applicationProcess=APP_proc'"
cmd1="curl -s -k -u admin:****** 'https://satya-inspiron-5566:8445/cli/agentCLI/info?agent=myAgent'"
cmd2="curl -s -k -u admin:****** 'https://satya-inspiron-5566:8445/cli/applicationProcess/info?application=MyApps&applicationProcess=APP_proc'"
cmd3="curl -s -k -u admin:****** 'https://satya-inspiron-5566:8445/cli/agentCLI/info?agent=myAgent'"

t1=threading.Thread(target=task,args=[cmd])
t2=threading.Thread(target=task, args=[cmd1])
t3=threading.Thread(target=task, args=[cmd2])
t4=threading.Thread(target=task, args=[cmd3])

t1.start()
t2.start()
t3.start()
t4.start()

Script output

Execution of multi-thread script with time command shows time reduces by half. This is because all four Rest commands execute by thread in parallel.

As a result it utilizes maximum processor capacity. Therefore, make execution faster.

 

>time python thread_ucd.py 
.....
.....
real	0m0.246s
user	0m0.101s
sys	0m0.025s

 

You can see this is faster as compare to the normal script execution. For more generic information on multi-thread refer Wikipedia.

 

You May Also Like..

How to concatenate string in Python

How to concatenate string in Python

How to concatenate string in Python: Concatenating a string is very important feature in any computer programming language. Python provide […]

Read and write CSV file using python

Read and write CSV file using python : CSV file, which is comma separated file, is the most popular form […]

How to create socket in Python programming

How to create socket in Python programming

How to create socket in Python programming: Socket is network interface, where two nodes in a network connect and communicate. […]

Leave a Reply

Your email address will not be published. Required fields are marked *