How To Use Python Thread Example

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

1. Python Thread Example Overview.

  1. There are 6 python modules in this example as below.
    python-thread-example-in-eclipse-pydev-project
  2. The TimeUtil module ( TimeUtil.py file ) is a tool module that provides class static methods ( decorated with @staticmethod ) to return the current time in string format. This module is used in other modules. Below is this module’s source code.
  3. 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.

  1. The example module ThreadModuleExample ( ThreadModuleExample.py file ) uses the python _thread module to create and manage thread. Below are the steps about how to use it.
  2. First, you should import the python _thread module into your python source code.
    import _thread
  3. 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))
  4. Create thread lock object use _thread.allocate_lock() function, then acquire it to lock the thread resources. If do not lock the thread explicitly, then the main thread will exit before the child thread execute completely.
    # Allocate the thread lock object.
    lock1 = _thread.allocate_lock()
    
    # Lock the thread resources.    
    lock1.acquire()
  5. 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.
    '''
    
    @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()
  6. Below is the console output for the thread_without_lock function. This function does not use thread lock.
  7. There are two threads 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 the below output message, you can see that when the main thread function is complete, the thread_1 executes complete but thread_2 does not execute completely.
    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
  8. 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.

  1. Below example module all use another python thread module threading and it’s Thread, Lock class.
  2. ThreadingModuleTargetFunctionExample ( ThreadingModuleTargetFunctionExample.py )
  3. ThreadingModuleTargetClassExample ( ThreadingModuleTargetClassExample.py )
  4. ThreadingLockExample ( ThreadingLockExample.py )
  5. CustomThreadingClassExample ( CustomThreadingClassExample.py )

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 another 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 invoking it’s start method.
    thread_1.start()
  4. If you want the main thread to exit after all child threads execution is complete, then you should call the thread object’s join() method.
    thread_1.join()
  5. If you want the child thread to execute in order, then you should create 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.

  1. This example will use a python function as the target parameter value when creating threading.Thread object.
  2. 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()
  3. Below is above example execution output. You can see the main thread will exit only when all child thread execution is 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.

  1. 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.

  1. You can create your own thread class by extends the threading.Thread class.
  2. The CustomThreadingClassExample module ( CustomThreadingClassExample.py ) is an example.
  3. The custom thread class must override the threading.Thread class’s run method. When the thread starts, 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()
  4. Below is the 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.

  1. 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()
  2. 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
0 0 votes
Article Rating
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x