How To Use Python Thread Example

There are two method to implement thread programming in python. 1. Use _thread module. 2. Use threading module’s Thread and Lock class. This article will show you examples about how to use them to implement thread programming in python.

1. Python Thread Example Overview.

python thread example in eclipse pydev project

There are 6 python modules in this example. The TimeUtil module ( TimeUtil.py file ) is a tool module that provide class static methods ( decorated with @staticmethod ) to return current time in string format. This module is used in other modules. Below is this module’s source code.

TimeUtil.py

from datetime import datetime

class TimeUtil(object):
    
    # The default time string format.
    date_time_str_format = '%y-%M-%d %H:%M:%S'

    # This function will convert date_time to string text with the default date_time_str_format. 
    @staticmethod   
    def convert_datetime_to_str(date_time):
        
        ret = datetime.strftime(date_time, TimeUtil.date_time_str_format)
        
        return ret
    
    # This function will get now time string format text.
    @staticmethod
    def get_now_time_str():
        
        now = datetime.now()
        
        now_str = TimeUtil.convert_datetime_to_str(now)
        
        return now_str

2. Use Python _thread Module.

The example module ThreadModuleExample ( ThreadModuleExample.py file ) use python _thread module to create and manage thread. Below is the steps about how to use it.

  1. First you should import python _thread module into your python source code.
    import _thread
  2. Call _thread module’s start_new_thread() function to create and start a new thread.
    _thread.start_new_thread(thread_util.thread_invoke_function, (1, 2, None))
  3. Create thread lock object use _thread.allocate_lock() function, then acquire it to lock the thread resources. If do not lock thread explicitly, then main thread will exit before child thread execute complete.
    # Allocate the thread lock object.
    lock1 = _thread.allocate_lock()
    
    # Lock the thread resources.    
    lock1.acquire()
  4. Below is the example source code, the source file name is ThreadModuleExample.py, there are two global functions ( thread_use_lock(), thread_without_lock() ) and one python class ( ThreadUtil ) in it.
    '''
    Created on Jan 8, 2020
    
    @author: songzhao
    '''
    
    # Import _thread module.
    import _thread
    
    # Import time class.
    import time 
    
    from com.dev2qa.example.thread.TimeUtil import TimeUtil
        
    
    ''' This class provide some useful methods for _thread module to use. '''
    class ThreadUtil(object):
         
            
        ''' This function will be invoked when use _thread module start_new_thread method to start a new thread.
            
            thread_number : is the thread number.
            
            sleep_time_seconds : is the seconds value that this thread will sleep.
            
            thread_lock : is the thread lock object.
        '''
        def thread_invoke_function(self, thread_number, sleep_time_seconds, thread_lock):
            
            print('Thread ', thread_number, ' start at : ', TimeUtil.get_now_time_str())
            
            print('Thread ', thread_number, ' will sleep ', sleep_time_seconds, ' seconds. ')
            
            time.sleep(sleep_time_seconds)
            
            if(thread_lock):
                
                thread_lock.release()
                
                print('Thread ', thread_number, ' is released.')
            
            print('Thread ', thread_number, ' stop at : ', TimeUtil.get_now_time_str())
         
    
    ''' This method will start two thread, and each thread do not lock resources. '''     
    def thread_without_lock():
    
        thread_util = ThreadUtil()       
               
        print('Main thread start at : ', TimeUtil.get_now_time_str())  
         
        # Start first thread, this thread will sleep 2 seconds. 
        thread_1 = _thread.start_new_thread(thread_util.thread_invoke_function, (1, 2, None))
        
        # print(thread_1)
        
        # Start the second thread, this thread will sleep 3 seconds.
        thread_2 = _thread.start_new_thread(thread_util.thread_invoke_function, (2, 5, None))
        
        # print(thread_2)
        
        # The main thread will sleep 2.5 seconds, so the second thread will be killed without finish task.
        time.sleep(3)
        
        print('Main thread stop at : ', TimeUtil.get_now_time_str())
        
    
    def thread_use_lock():
        
         # Create an instance of ThreadModuleUtil class.
        thread_util = ThreadUtil()
               
        print('Main thread start at : ', TimeUtil.get_now_time_str())
        
        thread_locks = []
         
        # Allocate and acquire thread lock. 
        lock1 = _thread.allocate_lock()
        
        lock1.acquire()
        
        thread_locks.append(lock1)
         
        # Start first thread, this thread will sleep 2 seconds. 
        thread_1 = _thread.start_new_thread(thread_util.thread_invoke_function, (1, 2, lock1))
        
        # print(thread_1)
        
        lock2 = _thread.allocate_lock()
        
        lock2.acquire()
        
        thread_locks.append(lock2)
        
        # Start the second thread, this thread will sleep 3 seconds.
        thread_2 = _thread.start_new_thread(thread_util.thread_invoke_function, (2, 5, lock2))
        
        # print(thread_2)    
        
        all_threads_are_released = False
        
        while not all_threads_are_released:
            
            has_locked_thread = False
            
            for lock in thread_locks:
                
                #print("lock.locked = ", lock.locked())
                
                if(lock.locked()):
                    
                    has_locked_thread = True
                 
            all_threads_are_released = not has_locked_thread      
            
            #print(' all_threads_are_released = ', all_threads_are_released)  
        
        # The main thread will sleep 2 seconds, so the second thread will be killed without finish task.
        time.sleep(2)
        
        print('Main thread stop at : ', TimeUtil.get_now_time_str())           
            
            
    if __name__ == '__main__':
        
        #thread_without_lock()
        
        thread_use_lock()
  5. Below is the console output for thread_without_lock function. This function do not use thread lock. There are two thread in this function, thread_1 will sleep 2 seconds and thread_2 will sleep 5 seconds. But the main thread function will only sleep 3 seconds. So from below output message, you can see that when the main thread function complete, the thread_1 execute complete but thread_2 do not execute complete.
    Main thread start at :  20-28-15 20:28:43
    
    Thread  1  start at :   20-28-15 20:28:43
    
    Thread  2  start at : 20-28-15 20:28:43
    
    Thread  1  will sleep 2 seconds. 
    
    Thread  2  will sleep 5 seconds. 
    
    Thread  1  stop at :  20-28-15 20:28:45
    
    Main thread stop at :  20-28-15 20:28:46
  6. Below is function thread_use_lock output text. Because this function use thread lock, so you can see both thread_1 and thread_2 execute complete then main thread exit.
    Main thread start at :  20-38-15 20:38:13
    
    Thread 1 start at :  20-38-15 20:38:13
    
    Thread 1 will sleep  2  seconds. 
    
    Thread 2 start at :  20-38-15 20:38:13
    
    Thread 2 will sleep  5  seconds. 
    
    Thread 1 is released.
    
    Thread 1 stop at :  20-38-15 20:38:15
    
    Thread 2 is released.
    
    Thread 2 stop at :  20-38-15 20:38:18
    
    Main thread stop at :  20-38-15 20:38:20

3. Use Python threading Module Thread, Lock Class.

Below example module all use another python thread module threading and it’s Thread, Lock class.

  1. ThreadingModuleTargetFunctionExample ( ThreadingModuleTargetFunctionExample.py )
  2. ThreadingModuleTargetClassExample ( ThreadingModuleTargetClassExample.py )
  3. ThreadingLockExample ( ThreadingLockExample.py )
  4. CustomThreadingClassExample ( CustomThreadingClassExample.py )
READ :   How To Install Python Packages Using Requirements Text File

3.1 Use Python threading Module To Create Thread Steps.

  1. Import python threading module.

    import threading
  2. Create an instance of threading.Thread class. The Thread class’s initializer method’s target parameter can be a function or an instance of other python class.
    # Create the thread object, the target parameter is a function.
    thread_1 = threading.Thread(target=tool.thread_invoke_function, args=(1, 2))
    
    # Create the thread execution target object.
    thread_target_class_1 = ThreadTargetClass(target_func=thread_invoke_function, params={'thread_number' : 1, 'sleep_time_seconds' : 2})    
             
    # Start first thread, this thread will sleep 2 seconds. 
    thread_1 = Thread(target=thread_target_class_1)
    
    
  3. Run the thread object by invoke it’s start method.
    thread_1.start()
  4. If you want the main thread exit after all child threads execution complete, then you should call the thread object’s join() method.
    thread_1.join()
  5. If you want child thread execute in order, then you should create a threading.Lock object and then call the Lock object’s acquire() method to get the lock, and release() method to release the lock.
    thread_lock = Lock()
    
    thread_lock.acquire()
    ......
    ......
    thread_lock.release()

3.2 Use Python Function As Thread Class’s Target Parameter Example.

This example will use a python function as target parameter value when create a threading.Thread object.

ThreadingModuleTargetFunctionExample ( ThreadingModuleTargetFunctionExample.py )

'''
Created on Jan 8, 2020

@author: songzhao

'''

import threading

import time

from com.dev2qa.example.thread.TimeUtil import TimeUtil    

class ThreadingUtil(object):
    
    '''
        This function will be invoked when use _thread module start_new_thread method to start a new thread.
        
        thread_number : is the thread number.
        
        sleep_time_seconds : is the seconds value that this thread will sleep.
        
    '''
    def thread_invoke_function(self, thread_number, sleep_time_seconds):
        
        print('Thread ', thread_number, ' start at : ', TimeUtil.get_now_time_str())
        
        print('Thread ', thread_number, ' will sleep ', sleep_time_seconds, ' seconds. ')
        
        time.sleep(sleep_time_seconds)
        
        print('Thread ', thread_number, ' stop at : ', TimeUtil.get_now_time_str())
        
        
        
def main_target_function():
    
    tool = ThreadingUtil()
                       
    print('Main thread start at : ', TimeUtil.get_now_time_str())
        
    thread_array = []
         
    # Start first thread, this thread will sleep 2 seconds. 
    thread_1 = threading.Thread(target=tool.thread_invoke_function, args=(1, 2))
        
    # Set the thread object as daemon thread.
    thread_1.setDaemon(True)
        
    thread_array.append(thread_1)
        
    # Start the second thread, this thread will sleep 3 seconds.
    thread_2 = threading.Thread(target=tool.thread_invoke_function, args=(2, 5))
        
    thread_2.setDaemon(False)
        
    thread_array.append(thread_2)
        
        
    for thread in thread_array:
            
        thread.start()
            
    ''' Below code will make main thread wait for all child threads stop then exit. If you comment below code, main thread will stop first, but child thread still run.'''
    for thread in thread_array:
            
        thread.join()    

    time.sleep(2)
        
    print('Main thread stop at : ', TimeUtil.get_now_time_str())
                
        
if __name__ == '__main__':
    
    main_target_function()

Below is above example execution output. You can see the main thread will exit only when all child thread execution complete.

Main thread start at :  20-40-15 22:40:53

Thread  1  start at :  20-40-15 22:40:53

Thread  1  will sleep  2  seconds. 

Thread  2  start at :  20-40-15 22:40:53

Thread  2  will sleep  5  seconds. 

Thread  1  stop at :  20-40-15 22:40:55

Thread  2  stop at :  20-40-15 22:40:58

Main thread stop at :  20-41-15 22:41:00

3.3 Use Python Class Instance As Thread Class’s Target Parameter Example.

ThreadingModuleTargetClassExample ( ThreadingModuleTargetClassExample.py )

'''
Created on Jan 9, 2020

@author: songzhao
'''

import time

from threading import Thread
   
from com.dev2qa.example.thread.TimeUtil import TimeUtil   
    
''' This class is the Thread target run class, when the thread execute, the class's __call__ method will be invoked.  '''
class ThreadTargetClass(object):
    
    def __init__(self, target_func, params):
        
        self.target_func = target_func
        
        self.params = params
        
    
    def __call__(self):
        
        self.target_func(**self.params)
        
    
def thread_invoke_function(**params):
    
    thread_number = params['thread_number']
    
    sleep_time_seconds = params['sleep_time_seconds']
        
    print('Thread ', thread_number, ' start at : ', TimeUtil.get_now_time_str())
        
    print('Thread ', thread_number, ' will sleep ', sleep_time_seconds, ' seconds. ')
        
    time.sleep(sleep_time_seconds)
        
    print('Thread ', thread_number, ' stop at : ', TimeUtil.get_now_time_str())
    
    
def main_target_class():
                       
    print('Main thread start at : ', TimeUtil.get_now_time_str())
        
    thread_array = []
      
    # Create the thread execution target object. 
    thread_target_class_1 = ThreadTargetClass(target_func=thread_invoke_function, params={'thread_number' : 1, 'sleep_time_seconds' : 2})    
         
    # Start first thread, this thread will sleep 2 seconds. 
    thread_1 = Thread(target=thread_target_class_1)
        
    # Set the thread object as daemon thread.
    thread_1.setDaemon(True)
        
    thread_array.append(thread_1)
        
    # Start the second thread, this thread will sleep 3 seconds.
    thread_target_class_2 = ThreadTargetClass(target_func=thread_invoke_function, params={'thread_number' : 2, 'sleep_time_seconds' : 5})   
    
    thread_2 = Thread(target=thread_target_class_2)
        
    thread_2.setDaemon(False)
        
    thread_array.append(thread_2)
        
        
    for thread in thread_array:
            
        thread.start()
            
    ''' Below code will make main thread wait for all child threads stop then exit. If you comment below code, main thread will stop first, but child thread still run.'''
    for thread in thread_array:
            
        thread.join()    

    time.sleep(2)
        
    print('Main thread stop at : ', TimeUtil.get_now_time_str())
    
    
if __name__ == '__main__':
    
    main_target_class()

3.4 Extend Python threading.Thread Class.

You can create your own thread class by extend the threading.Thread class. The CustomThreadingClassExample module ( CustomThreadingClassExample.py ) is an example. The custom thread class must override the threading.Thread class’s run method. When the thread start, the run method will be executed.

'''
Created on Jan 8, 2020

@author: songzhao
'''

import time

from threading import Thread

from com.dev2qa.example.thread.TimeUtil import TimeUtil
    

class MyThread(Thread):    
    
    def __init__(self, thread_id_number=None, thread_sleep_seconds=None):
        
        Thread.__init__(self)
        
        self.thread_id_number = thread_id_number
                
        self.thread_sleep_seconds = thread_sleep_seconds
                  
    
    ''' Override threading.Thread run method. This method will be invoked when the thread start. '''
    def run(self):
        
        self.thread_invoke_function()
    
        
        
    def thread_invoke_function(self):
        
        print('MyThread ', self.thread_id_number, ' start at : ', TimeUtil.get_now_time_str())
        
        print('MyThread ', self.thread_id_number, ' is daemon thread : ', self.isDaemon(), ', it will sleep ', self.thread_sleep_seconds, ' seconds. ')
        
        time.sleep(self.thread_sleep_seconds)
        
        print('MyThread ', self.thread_id_number, ' stop at : ', TimeUtil.get_now_time_str())
        
            
        
def main():
                       
        print('Main thread start at : ', TimeUtil.get_now_time_str())
        
        thread_array = []
         
        # Start first thread, this thread will sleep 2 seconds. 
        thread_1 = MyThread(thread_id_number=1, thread_sleep_seconds=2)
        
        thread_1.setDaemon(False)
        
        thread_array.append(thread_1)
        
        # Start the second thread, this thread will sleep 3 seconds.
        thread_2 = MyThread(thread_id_number=2, thread_sleep_seconds=5)
        
        thread_2.setDaemon(True)
        
        thread_array.append(thread_2)
        
        
        for thread in thread_array:
            
            thread.start()
            
        ''' Below code will make main thread wait for all child threads stop then exit. 
         If you comment below code, main thread will stop first, but child thread still run.'''
        for thread in thread_array:
            
            thread.join()    

        
        time.sleep(2)
        
        print('Main thread stop at : ', TimeUtil.get_now_time_str())  
        
        
if __name__ == '__main__':
    
    main()

Below is above example execution result.

Main thread start at :  20-56-15 22:56:33

MyThread  1  start at :  20-56-15 22:56:33

MyThread  2  start at :  20-56-15 22:56:33

MyThread  2  is daemon thread :  True , it will sleep  5  seconds. 

MyThread  1  is daemon thread :  False , it will sleep  2  seconds. 

MyThread  1  stop at :  20-56-15 22:56:35

MyThread  2  stop at :  20-56-15 22:56:38

Main thread stop at :  20-56-15 22:56:40

3.5 Use threading.Lock Object To Lock Child Thread Execution Order.

ThreadingLockExample ( ThreadingLockExample.py )

import time

from threading import Thread, Lock

from com.dev2qa.example.thread.TimeUtil import TimeUtil
  
# Create a global thread Lock object.    
thread_lock = Lock()    

class MyThread(Thread):    
    
    def __init__(self, thread_id_number=None, thread_sleep_seconds=None):
        
        Thread.__init__(self)
        
        self.thread_id_number = thread_id_number
                
        self.thread_sleep_seconds = thread_sleep_seconds
        
    
    ''' Override threading.Thread run method. This method will be invoked when the thread start. '''
    def run(self):
        
        # Acquire the thread lock.
        thread_lock.acquire()
        
        print('Thread ', self.thread_id_number, ' start at : ', TimeUtil.get_now_time_str())
        
        print('Thread ', self.thread_id_number, ' is daemon thread : ', self.isDaemon(), ', it will sleep ', self.thread_sleep_seconds, ' seconds. ')
        
        time.sleep(self.thread_sleep_seconds)
        
        print('Thread ', self.thread_id_number, ' stop at : ', TimeUtil.get_now_time_str())
        
        # Release the thread lock.
        thread_lock.release()
        
            
        
def main():
                       
    print('Main thread start at : ', TimeUtil.get_now_time_str())
        
    thread_array = []
         
    # Start first thread, this thread will sleep 2 seconds. 
    thread_1 = MyThread(thread_id_number=1, thread_sleep_seconds=2)
        
    thread_1.setDaemon(False)
        
    thread_array.append(thread_1)
        
    # Start the second thread, this thread will sleep 3 seconds.
    thread_2 = MyThread(thread_id_number=2, thread_sleep_seconds=5)
        
    thread_2.setDaemon(True)
        
    thread_array.append(thread_2)
        
        
    for thread in thread_array:
            
        thread.start()
            
    ''' Below code will make main thread wait for all child threads stop then exit. 
        If you comment below code, main thread will stop first, but child thread still run.'''
    for thread in thread_array:
            
        thread.join()    

        
    time.sleep(2)
        
    print('Main thread stop at : ', TimeUtil.get_now_time_str())  
        
        
if __name__ == '__main__':
    
    main()

Below is above example execution result, you can see the child thread execution is ordered.

Main thread start at :  20-47-15 22:47:23

Thread  1  start at :  20-47-15 22:47:23

Thread  1  is daemon thread :  False , it will sleep  2  seconds. 

Thread  1  stop at :  20-47-15 22:47:25


Thread  2  start at :  20-47-15 22:47:25

Thread  2  is daemon thread :  True , it will sleep  5  seconds. 

Thread  2  stop at :  20-47-15 22:47:30

Main thread stop at :  20-47-15 22:47:32

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.