Python中使用threading.Event協(xié)調(diào)線程的運(yùn)行詳解
threading.Event機(jī)制類似于一個(gè)線程向其它多個(gè)線程發(fā)號(hào)施令的模式,其它線程都會(huì)持有一個(gè)threading.Event的對(duì)象,這些線程都會(huì)等待這個(gè)事件的“發(fā)生”,如果此事件一直不發(fā)生,那么這些線程將會(huì)阻塞,直至事件的“發(fā)生”。
對(duì)此,我們可以考慮一種應(yīng)用場(chǎng)景(僅僅作為說(shuō)明),例如,我們有多個(gè)線程從Redis隊(duì)列中讀取數(shù)據(jù)來(lái)處理,這些線程都要嘗試去連接Redis的服務(wù),一般情況下,如果Redis連接不成功,在各個(gè)線程的代碼中,都會(huì)去嘗試重新連接。
如果我們想要在啟動(dòng)時(shí)確保Redis服務(wù)正常,才讓那些工作線程去連接Redis服務(wù)器,那么我們就可以采用threading.Event機(jī)制來(lái)協(xié)調(diào)各個(gè)工作線程的連接操作:
主線程中會(huì)去嘗試連接Redis服務(wù),如果正常的話,觸發(fā)事件,各工作線程會(huì)嘗試連接Redis服務(wù)。
為此,我們可以寫下如下的程序:
import threadingimport timeimport logging logging.basicConfig(level=logging.DEBUG, format=’(%(threadName)-10s) %(message)s’,) def worker(event): logging.debug(’Waiting for redis ready...’) event.wait() logging.debug(’redis ready, and connect to redis server and do some work [%s]’, time.ctime()) time.sleep(1) readis_ready = threading.Event()t1 = threading.Thread(target=worker, args=(readis_ready,), name=’t1’)t1.start() t2 = threading.Thread(target=worker, args=(readis_ready,), name=’t2’)t2.start() logging.debug(’first of all, check redis server, make sure it is OK, and then trigger the redis ready event’)time.sleep(3) # simulate the check progress readis_ready.set()
運(yùn)行這個(gè)程序:
(t1 ) Waiting for redis ready...(t2 ) Waiting for redis ready...(MainThread) first of all, check redis server, make sure it is OK, and then trigger the redis ready event(t2 ) redis ready, and connect to redis server and do some work [Wed Nov 5 12:45:03 2014](t1 ) redis ready, and connect to redis server and do some work [Wed Nov 5 12:45:03 2014]
t1和t2線程開始的時(shí)候都阻塞在等待redis服務(wù)器啟動(dòng)的地方,一旦主線程確定了redis服務(wù)器已經(jīng)正常啟動(dòng),那么會(huì)觸發(fā)redis_ready事件,各個(gè)工作線程就會(huì)去連接redis去做相應(yīng)的工作。
threading.Event的wait方法還接受一個(gè)超時(shí)參數(shù),默認(rèn)情況下如果事件一直沒有發(fā)生,wait方法會(huì)一直阻塞下去,而加入這個(gè)超時(shí)參數(shù)之后,如果阻塞時(shí)間超過這個(gè)參數(shù)設(shè)定的值之后,wait方法會(huì)返回。
對(duì)應(yīng)于上面的應(yīng)用場(chǎng)景,如果Redis服務(wù)器一致沒有啟動(dòng),我們希望子線程能夠打印一些日志來(lái)不斷地提醒我們當(dāng)前沒有一個(gè)可以連接的Redis服務(wù),我們就可以通過設(shè)置這個(gè)超時(shí)參數(shù)來(lái)達(dá)成這樣的目的:
import threadingimport timeimport logging logging.basicConfig(level=logging.DEBUG, format=’(%(threadName)-10s) %(message)s’,) def worker(event): while not event.is_set(): logging.debug(’Waiting for redis ready...’) event.wait(1) logging.debug(’redis ready, and connect to redis server and do some work [%s]’, time.ctime()) time.sleep(1) readis_ready = threading.Event()t1 = threading.Thread(target=worker, args=(readis_ready,), name=’t1’)t1.start() t2 = threading.Thread(target=worker, args=(readis_ready,), name=’t2’)t2.start() logging.debug(’first of all, check redis server, make sure it is OK, and then trigger the redis ready event’)time.sleep(3) # simulate the check progress readis_ready.set()
與前面的無(wú)限阻塞版本唯一的不同就是,我們?cè)诠ぷ骶€程中加入了一個(gè)while循環(huán),直到redis_ready事件觸發(fā)之后才會(huì)結(jié)束循環(huán),wait方法調(diào)用會(huì)在1秒的超時(shí)后返回,這樣,我們就可以看到各個(gè)工作線程在系統(tǒng)啟動(dòng)的時(shí)候等待redis_ready的同時(shí),會(huì)記錄一些狀態(tài)信息。
以下是這個(gè)程序的運(yùn)行結(jié)果:
(t1 ) Waiting for redis ready...(t2 ) Waiting for redis ready...(MainThread) first of all, check redis server, make sure it is OK, and then trigger the redis ready event(t2 ) Waiting for redis ready...(t1 ) Waiting for redis ready...(t2 ) Waiting for redis ready...(t1 ) Waiting for redis ready...(t2 ) redis ready, and connect to redis server and do some work [Wed Nov 5 13:55:46 2014](t1 ) redis ready, and connect to redis server and do some work [Wed Nov 5 13:55:46 2014]
這樣,我們就可以在等待Redis服務(wù)啟動(dòng)的同時(shí),看到工作線程里正在等待的情況。
以上這篇Python中使用threading.Event協(xié)調(diào)線程的運(yùn)行詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python爬蟲實(shí)戰(zhàn)之制作屬于自己的一個(gè)IP代理模塊2. asp批量添加修改刪除操作示例代碼3. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)財(cái)務(wù)記賬管理系統(tǒng)4. css代碼優(yōu)化的12個(gè)技巧5. 如何在jsp界面中插入圖片6. Vue element ui用戶展示頁(yè)面的實(shí)例7. Ajax返回值類型與用法實(shí)例分析8. 使用FormData進(jìn)行Ajax請(qǐng)求上傳文件的實(shí)例代碼9. .NET6打包部署到Windows Service的全過程10. HTML 絕對(duì)路徑與相對(duì)路徑概念詳細(xì)
