python基于event實現(xiàn)線程間通信控制
這篇文章主要介紹了python基于event實現(xiàn)線程間通信控制,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
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()
運(yùn)行后顯示:
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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
利用Python matplotlib繪制風(fēng)能玫瑰圖
這篇文章主要給大家介紹了關(guān)于如何利用Python matplotlib繪制風(fēng)能玫瑰圖的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03TensorFlow繪制loss/accuracy曲線的實例
今天小編就為大家分享一篇TensorFlow繪制loss/accuracy曲線的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01python目標(biāo)檢測數(shù)據(jù)增強(qiáng)的代碼參數(shù)解讀及應(yīng)用
這篇文章主要為大家介紹了python目標(biāo)檢測數(shù)據(jù)增強(qiáng)的代碼參數(shù)解讀及應(yīng)用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05詳解python函數(shù)的閉包問題(內(nèi)部函數(shù)與外部函數(shù)詳述)
這篇文章主要介紹了python函數(shù)的閉包問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05使用bandit對目標(biāo)python代碼進(jìn)行安全函數(shù)掃描的案例分析
這篇文章主要介紹了使用bandit對目標(biāo)python代碼進(jìn)行安全函數(shù)掃描,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01