簡體   English   中英

Python - 如何在其他線程的線程上暫停?

[英]Python - How can I pause at thread by other thread?

我對顯示等待消息感到困惑。

對於我的程序,
第 1 步,它不斷循環顯示等待消息。
步驟2,如果trigger_file存在,則停止等待消息並運行main_process()
步驟 3,完成main_process ,它再次顯示等待消息。

我嘗試使用變量waiting來停止等待消息,但它不起作用我不確定如何在這種情況下使用 async/await 函數和 multithreadubg。
謝謝

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!")

預期輸出:

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

最后我用threading.Lock來解決這個問題。 acquire鎖並在完成函數后release鎖。

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM