python基于event實現(xiàn)線程間通信控制
更新時間:2020年01月13日 09:17:54 作者:Iceberg_710815
這篇文章主要介紹了python基于event實現(xiàn)線程間通信控制,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
這篇文章主要介紹了python基于event實現(xiàn)線程間通信控制,文中通過示例代碼介紹的非常詳細,對大家的學(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()
運行后顯示:
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)文章
Flask?+?MySQL如何實現(xiàn)用戶注冊,登錄和登出的項目實踐
本文主要介紹了Flask?+?MySQL?如何實現(xiàn)用戶注冊,登錄和登出的項目實踐,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
Python 由字符串函數(shù)名得到對應(yīng)的函數(shù)(實例講解)
下面小編就為大家?guī)硪黄狿ython 由字符串函數(shù)名得到對應(yīng)的函數(shù)(實例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08

