Search code examples
pythonmultithreadingasync-awaitloading

Python - How can I pause at thread by other thread?


I am confused for showing waiting message.

For my program,
Step 1, it keep looping to show the waiting message.
Step 2, If the trigger_file exist, it stop the waiting message and run main_process()
Step 3, After finishing main_process, it show the waiting message again.

I tried to use variable waiting to stop the waiting message but it is not working I am not sure how to use async/await function and multithreadubg for this case.
Thank you

import os
import time
import threading


waiting = True
trigger_file = r'D:\Desktop\OK'



def main_process():
    print('1')
    time.sleep(5)
    print('2')
    time.sleep(5)
    print('3')
    time.sleep(5)
    print('4')
    time.sleep(5)
    print('5')

def print_waiting():   # animation of waiting
    while(waiting):
        for loading_symbol in ['|','/','-','\\','|','/','-','\\','|']:
            print('\r[INFO] Waiting for trigger... '+loading_symbol,end="")
            time.sleep(0.2)


def triggerListener():  # trigger a function if the file exist
    while(True):
        if os.path.exists(trigger_file):
            global waiting
            waiting=False
            print('\n[INFO] main process start')
            main_process()
            waiting=True





if __name__ == "__main__":
    # creating thread
    t1 = threading.Thread(target=print_waiting)
    t2 = threading.Thread(target=triggerListener)
  
    # starting thread 1
    t1.start()
    # starting thread 2
    t2.start()
  
    # wait until thread 1 is completely executed
    t1.join()
    # wait until thread 2 is completely executed
    t2.join()
  
    # both threads completely executed
    print("Done!")

Expected Output:

[INFO] Waiting for trigger... -
[INFO] main process start
1
2
3
4
5
[INFO] Waiting for trigger... -

Solution

  • Finally, I use threading.Lock to solve the problem. acquire the lock and release the lock after finishing the function.

    class Print_waiting(threading.Thread):
        def __init__(self,lock):
            threading.Thread.__init__(self)
            self.running = True
            self.lock = lock
    
        def run(self):   # animation of waiting
            while self.running:
                for loading_symbol in ['|','/','-','\\','|','/','-','\\','|']:
                    self.lock.acquire()
                    print('\r[INFO] Waiting for trigger... '+loading_symbol,end="")
                    self.lock.release()
                    time.sleep(0.2)
    
        def stop(self):
            self.running = False
        
    
    
    class TriggerListener(threading.Thread):
        def __init__(self,lock):
            threading.Thread.__init__(self)
            self.running = True
            self.lock = lock  # for mutex
    
        def run(self):   # trigger a function if the file exist
            while(self.running):
                if os.path.exists(trigger_file):
                    self.lock.acquire()
                    print('\n[INFO] main process start')
                    Program.main()
                    self.lock.release()
    
        def stop(self):
            self.running = False
    
    
    
    
    if __name__ == "__main__":
        lock = threading.Lock()
        waiting_anime = Print_waiting(lock)
        Trigger_RPA = TriggerListener(lock)
    
        Trigger_RPA.start()
        waiting_anime.start()
    
        Trigger_RPA.join()
        waiting_anime.join()