python基于event實現(xiàn)線程間通信控制
更新時間:2020年01月13日 09:17:54 作者:Iceberg_710815
這篇文章主要介紹了python基于event實現(xiàn)線程間通信控制,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
這篇文章主要介紹了python基于event實現(xiàn)線程間通信控制,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
import threading,time class Boss(threading.Thread): def run(self): print("We must work today!") event.isSet() or event.set() time.sleep(5) print("You can go home right now!") event.isSet() or event.set() class Worker(threading.Thread): def run(self): event.wait() print("Oh,my god!!!") time.sleep(1) event.clear() event.wait() print("Oh,yeah!!!") if __name__ == "__main__": event = threading.Event() threads = [] for i in range(5): threads.append(Worker()) threads.append(Boss()) for t in threads: t.start() for t in threads: t.join()
運行后顯示:
We must work today! Oh,my god!!! Oh,my god!!! Oh,my god!!! Oh,my god!!! Oh,my god!!! You can go home right now! Oh,yeah!!! Oh,yeah!!! Oh,yeah!!! Oh,yeah!!! Oh,yeah!!! Process finished with exit code 0
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
TensorFlow繪制loss/accuracy曲線的實例
今天小編就為大家分享一篇TensorFlow繪制loss/accuracy曲線的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01python目標檢測數(shù)據(jù)增強的代碼參數(shù)解讀及應用
這篇文章主要為大家介紹了python目標檢測數(shù)據(jù)增強的代碼參數(shù)解讀及應用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05詳解python函數(shù)的閉包問題(內(nèi)部函數(shù)與外部函數(shù)詳述)
這篇文章主要介紹了python函數(shù)的閉包問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-05-05使用bandit對目標python代碼進行安全函數(shù)掃描的案例分析
這篇文章主要介紹了使用bandit對目標python代碼進行安全函數(shù)掃描,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01