Python Multithreading

What is thread?

Thread is just a simple program(also called light weight process) which can be executed concurrently.

What is multithreading?

  • Running multiple threads concurrently and shares global data space with main thread.
  • Threads can share information and communicates with each other more easily.
  • Locks are provided in python for synchronization in multiple threading.
  • Each thread has different state like beginning, execution, interrupted, sleeping, conclusion etc.

Creating thread using thread module

thread.start_new_thread(function, args[, kwargs])
Starts a new thread and returns identifier. here function is the name of the calling function and args is the list of function parameters as tuple and kwargs(keyword arguments) is optional parameter.
import thread                                                                   
import time                                                                     
import threading                                                                
                                                                                
def display_message(thread_name, delay):                                        
    count = 0                                                                   
    while count < 5:                                                            
        time.sleep(delay)                                                       
        count += 1                                                              
        print "%s: Count:%d / %s" % (thread_name, count, time.ctime(time.time()))
                                                                                
try:                                                                            
    thread.start_new_thread(display_message, ("Thread-1", 3, ))                 
    thread.start_new_thread(display_message, ("Thread-2", 2, ))                 
except:                                                                         
    print "Error"                                       
                                                                                
# print threading.enumerate()                                                     
                                                                                
input = raw_input("do you want to run multithreading demo?(y/n)")               
                                                                                
while(input in ["y", "Y","yes", "Yes"]):                                        
    pass     
Output:
$ python thread_test.py 
do you want to run multithreading demo?(y/n)y
Thread-2: Count:1 / Sun Sep  3 14:11:12 2017
Thread-1: Count:1 / Sun Sep  3 14:11:13 2017
Thread-2: Count:2 / Sun Sep  3 14:11:14 2017
Thread-1: Count:2 / Sun Sep  3 14:11:16 2017
Thread-2: Count:3 / Sun Sep  3 14:11:16 2017
Thread-2: Count:4 / Sun Sep  3 14:11:18 2017
Thread-1: Count:3 / Sun Sep  3 14:11:19 2017
Thread-2: Count:5 / Sun Sep  3 14:11:20 2017
Thread-1: Count:4 / Sun Sep  3 14:11:22 2017
Thread-1: Count:5 / Sun Sep  3 14:11:25 2017


Creating thread using threading module

  • threading module is same as thread module but it's have some additional methods.
  • Class threading.Thread is used to create a new thread.
  • threading.Thread class has start method which invokes run method and it can be overloaded.
  • join method is used to ensure that main thread waits till all threads will be completed.
#!/usr/bin/python                                                               
                                                                                
import random                                                                   
import time                                                                     
import threading                                                                
                                                                                
def display_message(count):                                                     
    random_sec = random.randint(1,4)                                            
    print "thread %d is going to sleep for %d seconds" % (count, random_sec)    
    time.sleep(random_sec)                                                      
    print "thread %d is resuming back" % count                                  
                                                                                
for count in range(5):                                                          
    thr = threading.Thread(target=display_message, args=(count,))               
    thr.start()      
Output:
$ python thread_test1.py
thread 0 is going to sleep for 3 seconds
thread 1 is going to sleep for 1 seconds
thread 2 is going to sleep for 3 seconds
thread 3 is going to sleep for 4 seconds
 thread 4 is going to sleep for 3 seconds
thread 1 is resuming back
thread 0 is resuming back
thread 2 is resuming back
thread 4 is resuming back
thread 3 is resuming back

Custom thread using threading module

This program is used to create a thread for each input number to check whether given number is odd or even.
#!/usr/bin/python                                                               
                                                                                
import threading                                                                
                                                                                
class Number(threading.Thread):                                                 
  def __init__(self, num):                                                      
    threading.Thread.__init__(self)                                             
    self.num = num                                                              
                                                                                
  def run(self):                                                                
      if self.num % 2 == 0:                                                     
          print "%d is even number\n" % self.num                                
      else:                                                                     
          print "%d is odd number\n" % self.num                                 
                                                                                
threads = []                                                                    
while True:                                                                     
    input = long(raw_input("Number(0 to exit): "))                              
    if(input == 0):                                                             
       break;                                                                   
    thread = Number(input)                                                      
    threads.append(thread)                                                      
    thread.start()                                                              
                                                                                
for thr in threads:                                                             
    thr.join()  
Output:
$ python thread_test2.py
Number(0 to exit): 8
8 is even number
Number(0 to exit): 
1
1 is odd number
Number(0 to exit): 
0

Synchronizing Threads

threading module providing simple locking mechanism to allow synchronizing threads.

Python program for multiple threads with updating file in synchronized method.

#!/usr/bin/python                                                               
# coding=utf-8                                                                  
                                                                                
import threading                                                                
import time                                                                     
import random                                                                   
                                                                                
class UpdateFile(threading.Thread):                                             
  def __init__(self, thread_name, msg, delay):                                  
    threading.Thread.__init__(self)                                             
    self.thread_name = thread_name                                              
    self.msg = msg                                                              
    self.delay = delay                                                          
                                                                                
  def run(self):                                                                
      file_msg = ("Thread '%s': delay: %d, message: "                           
                 "%s\n" % (self.thread_name, self.delay, self.msg))             
      print file_msg                                                            
      time.sleep(self.delay)                                                    
      thread_lock.acquire()                                                     
      f = open('file_name.txt', 'a+')                                           
      f.write(file_msg)                                                         
      f.close()                                                                 
      print("Thread '%s' is completed" % self.thread_name)                      
      thread_lock.release()                                                     
                                                                                
thread_lock = threading.Lock()                                                  
threads = []                                                                    
count = 1                                                                       
while True:                                                                     
    random_sec = random.randint(1,6)                                            
    input = raw_input("Message('q' to exit): ")                                 
    if(input == 'q'):                                                           
       break;                                                                   
    thread = UpdateFile(str(count), input, random_sec)                          
    threads.append(thread)                                                      
    thread.start()                                                              
    count += 1                                                                  
                                                                                
for thr in threads:                                                             
    thr.join()   
Output:
$ python thread_test3.py 
Message('q' to exit): hi
Thread '1': delay: 5, message: hi
Message('q' to exit): 
how r u?
Thread '2': delay: 1, message: how r u?
Message('q' to exit): 
Thread '2' is completed
Thread '1' is completed
file_name.txt will be created in current directory with below content
Thread '2': delay: 1, message: how r u?                                         
Thread '1': delay: 5, message: hi   

Python advantages in multithreading

  • Easy to run any function as thread in python.
  • Using threads are good for doing I/O bound tasks (networking, writing to disk, and so on).
  • Sharing data is simple across threads.
  • Multithreaded programs can run faster with multiple CPU's. performance will be improved.

Python disadvantage in multithreading

It is related to GIL (Global Interpreter Lock). Python threads cannot concurrently access the interpreter, there is one big lock Global Interpreter Lock.
Python can only execute one thread at a time for doing computation tasks. if starting many threads, all the threads depend on single lock (GIL).

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^