Event的用法
這篇對Event的用法講的比較清楚
https://www.cnblogs.com/hoojjack/p/6639128.html
其實所謂的Event控制線程/進程通信,Event中所謂的通信,并不是在流通具體的字符串信息,而是特指流通“狀態(tài)信息”
#-*- encoding:utf-8 -*- import sys reload(sys) sys.setdefaultencoding('utf-8') import multiprocessing import time #相關(guān)函數(shù),is_set(),wait(),set(),clear(),函數(shù) #set()設(shè)定為True,True的時候,其他進程可以進行通信 #clear()設(shè)定為False #is_set()查看當(dāng)前event是false還是True #wait指的是只有event是True的時候,當(dāng)前子線程才可以繼續(xù)運行def wait_for_event(e):print("wait_for_event: starting")e.wait()print("wait_for_event: e.is_set()->" + str(e.is_set()))def wait_for_event_timeout(e, t):print("wait_for_event_timeout:starting")e.wait(t)print("wait_for_event_timeout:e.is_set->" + str(e.is_set()))if __name__ == "__main__":e = multiprocessing.Event()w1 = multiprocessing.Process(name = "block",target = wait_for_event,args = (e,))#讓進程分別對應(yīng)一個函數(shù)w2 = multiprocessing.Process(name = "non-block",target = wait_for_event_timeout,args = (e, 2))#等待兩秒就不再阻塞w1.start()w2.start()time.sleep(3)e.set()print("main: event is set")
總結(jié)
- 上一篇: python必须使用try except
- 下一篇: Queue用法