
Python中使用Queue和Condition進行線程同步的方法
這篇文章主要介紹了Python中使用Queue模塊和Condition對象進行線程同步的方法,配合threading模塊下的線程編程進行操作的實例,需要的朋友可以參考下
Queue模塊保持線程同步
利用Queue對象先進先出的特性,將每個生產(chǎn)者的數(shù)據(jù)一次存入隊列,而每個消費者將依次從隊列中取出數(shù)據(jù)
import threading # 導(dǎo)入threading模塊
import Queue # 導(dǎo)入Queue模塊
class Producer(threading.Thread):# 定義生產(chǎn)者類
def __init__(self,threadname):
threading.Thread.__init__(self,name = threadname)
def run(self):
global queue # 聲明queue為全局變量
queue.put(self.getName()) # 調(diào)用put方法將線程名添加到隊列中
print self.getName(),'put ',self.getName(),' to queue'
class Consumer(threading.Thread):# 定義消費者類
def __init__(self,threadname):
threading.Thread.__init__(self,name = threadname)
def run(self):
global queue
print self.getName(),'get ',queue.get(),'from queue'#調(diào)用get方法獲取隊列中內(nèi)容
queue = Queue.Queue() # 生成隊列對象
plist = [] # 生成者對象列表
clist = [] # 消費者對象列表
for i in range(10):
p = Producer('Producer' + str(i))
plist.append(p) # 添加到生產(chǎn)者對象列表
for i in range(10):
c = Consumer('Consumer' + str(i))
clist.append(c) # 添加到消費者對象列表
for i in plist:
i.start() # 運行生產(chǎn)者線程
i.join()
for i in clist:
i.start() # 運行消費者線程
i.join()
######運行結(jié)果######
>>> Producer0 put Producer0 to queue
Producer1 put Producer1 to queue
Producer2 put Producer2 to queue
Producer3 put Producer3 to queue
Producer4 put Producer4 to queue
Producer5 put Producer5 to queue
Producer6 put Producer6 to queue
Producer7 put Producer7 to queue
Producer8 put Producer8 to queue
Producer9 put Producer9 to queue
Consumer0 get Producer0 from queue
Consumer1 get Producer1 from queue
Consumer2 get Producer2 from queue
Consumer3 get Producer3 from queue
Consumer4 get Producer4 from queue
Consumer5 get Producer5 from queue
Consumer6 get Producer6 from queue
Consumer7 get Producer7 from queue
Consumer8 get Producer8 from queue
Consumer9 get Producer9 from queue
Condition實現(xiàn)復(fù)雜的同步
使用Condition對象可以在某些事件觸發(fā)或者達到特定的條件后才處理數(shù)據(jù),Condition除了具有Lock對象的acquire方法和release方法外,
還有wait方法,notify方法,notifyAll方法等用于條件處理。
條件變量保持線程同步:threading.Condition()
wait():線程掛起,直到收到一個notify通知才會被喚醒繼續(xù)運行
notify():通知其他線程,那些掛起的線程接到這個通知之后會開始運行
notifyAll(): 如果wait狀態(tài)線程比較多,notifyAll的作用就是通知所有線程(這個一般用得少)
#coding:utf-8
import threading
import time
cond = threading.Condition()
class kongbaige(threading.Thread):
def __init__(self, cond, diaosiname):
threading.Thread.__init__(self, name = diaosiname)
self.cond = cond
def run(self):
self.cond.acquire() #獲取鎖
print self.getName() + ':一支穿云箭' #空白哥說的第一句話
self.cond.notify() #喚醒其他wait狀態(tài)的線程(通知西米哥 讓他說話)
#然后進入wait線程掛起狀態(tài)等待notify通知(等西米哥的回復(fù),接下來倆人就開始扯蛋)
self.cond.wait()
print self.getName() + ':山無棱,天地合,乃敢與君絕!'
self.cond.notify()
self.cond.wait()
print self.getName() + ':紫薇?。。?!(此處圖片省略)'
self.cond.notify()
self.cond.wait()
print self.getName() + ':是你'
self.cond.notify()
self.cond.wait()
#這里是空白哥說的最后一段話,接下來就沒有對白了
print self.getName() + ':有錢嗎 借點'
self.cond.notify() #通知西米哥
self.cond.release() #釋放鎖
class ximige(threading.Thread):
def __init__(self, cond, diaosiname):
threading.Thread.__init__(self, name = diaosiname)
self.cond = cond
def run(self):
self.cond.acquire()
self.cond.wait() #線程掛起(等西米哥的notify通知)
print self.getName() +':千軍萬馬來相見'
self.cond.notify() #說完話了notify空白哥wait的線程
self.cond.wait() #線程掛起等待空白哥的notify通知
print self.getName() + ':海可枯,石可爛,激情永不散!'
self.cond.notify()
self.cond.wait()
print self.getName() + ':爾康!??!(此處圖片省略)'
self.cond.notify()
self.cond.wait()
print self.getName() + ':是我'
self.cond.notify()
self.cond.wait()
#這里是最后一段話,后面空白哥沒接話了 所以說完就釋放鎖 結(jié)束線程
print self.getName() + ':滾'
self.cond.release()
kongbai = kongbaige(cond, ' ')
ximi = ximige(cond, '西米')
#尼瑪下面這2個啟動標(biāo)志是關(guān)鍵,雖然是空白哥先開的口,但是不能讓他先啟動,
#因為他先啟動的可能直到發(fā)完notify通知了,西米哥才開始啟動,
#西米哥啟動后會一直處于44行的wait狀態(tài),因為空白哥已經(jīng)發(fā)完notify通知了進入wait狀態(tài)了,
#而西米哥沒收到
#造成的結(jié)果就是2根線程就一直在那掛起,什么都不干,也不扯蛋了
ximi.start()
kongbai.start()
######運行結(jié)果######
:一支穿云箭
西米:千軍萬馬來相見
:山無棱,天地合,乃敢與君絕!
西米:??煽荩蔂€,激情永不散!
:紫薇!?。?!(此處圖片省略)
西米:爾康?。?!(此處圖片省略)
:是你
西米:是我
:有錢嗎 借點
西米:滾
數(shù)據(jù)分析咨詢請掃描二維碼
若不方便掃碼,搜微信號:CDAshujufenxi
LSTM 模型輸入長度選擇技巧:提升序列建模效能的關(guān)鍵? 在循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)家族中,長短期記憶網(wǎng)絡(luò)(LSTM)憑借其解決長序列 ...
2025-07-11CDA 數(shù)據(jù)分析師報考條件詳解與準(zhǔn)備指南? ? 在數(shù)據(jù)驅(qū)動決策的時代浪潮下,CDA 數(shù)據(jù)分析師認證愈發(fā)受到矚目,成為眾多有志投身數(shù) ...
2025-07-11數(shù)據(jù)透視表中兩列相乘合計的實用指南? 在數(shù)據(jù)分析的日常工作中,數(shù)據(jù)透視表憑借其強大的數(shù)據(jù)匯總和分析功能,成為了 Excel 用戶 ...
2025-07-11尊敬的考生: 您好! 我們誠摯通知您,CDA Level I和 Level II考試大綱將于 2025年7月25日 實施重大更新。 此次更新旨在確保認 ...
2025-07-10BI 大數(shù)據(jù)分析師:連接數(shù)據(jù)與業(yè)務(wù)的價值轉(zhuǎn)化者? ? 在大數(shù)據(jù)與商業(yè)智能(Business Intelligence,簡稱 BI)深度融合的時代,BI ...
2025-07-10SQL 在預(yù)測分析中的應(yīng)用:從數(shù)據(jù)查詢到趨勢預(yù)判? ? 在數(shù)據(jù)驅(qū)動決策的時代,預(yù)測分析作為挖掘數(shù)據(jù)潛在價值的核心手段,正被廣泛 ...
2025-07-10數(shù)據(jù)查詢結(jié)束后:分析師的收尾工作與價值深化? ? 在數(shù)據(jù)分析的全流程中,“query end”(查詢結(jié)束)并非工作的終點,而是將數(shù) ...
2025-07-10CDA 數(shù)據(jù)分析師考試:從報考到取證的全攻略? 在數(shù)字經(jīng)濟蓬勃發(fā)展的今天,數(shù)據(jù)分析師已成為各行業(yè)爭搶的核心人才,而 CDA(Certi ...
2025-07-09【CDA干貨】單樣本趨勢性檢驗:捕捉數(shù)據(jù)背后的時間軌跡? 在數(shù)據(jù)分析的版圖中,單樣本趨勢性檢驗如同一位耐心的偵探,專注于從單 ...
2025-07-09year_month數(shù)據(jù)類型:時間維度的精準(zhǔn)切片? ? 在數(shù)據(jù)的世界里,時間是最不可或缺的維度之一,而year_month數(shù)據(jù)類型就像一把精準(zhǔn) ...
2025-07-09CDA 備考干貨:Python 在數(shù)據(jù)分析中的核心應(yīng)用與實戰(zhàn)技巧? ? 在 CDA 數(shù)據(jù)分析師認證考試中,Python 作為數(shù)據(jù)處理與分析的核心 ...
2025-07-08SPSS 中的 Mann-Kendall 檢驗:數(shù)據(jù)趨勢與突變分析的有力工具? ? ? 在數(shù)據(jù)分析的廣袤領(lǐng)域中,準(zhǔn)確捕捉數(shù)據(jù)的趨勢變化以及識別 ...
2025-07-08備戰(zhàn) CDA 數(shù)據(jù)分析師考試:需要多久?如何規(guī)劃? CDA(Certified Data Analyst)數(shù)據(jù)分析師認證作為國內(nèi)權(quán)威的數(shù)據(jù)分析能力認證 ...
2025-07-08LSTM 輸出不確定的成因、影響與應(yīng)對策略? 長短期記憶網(wǎng)絡(luò)(LSTM)作為循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)的一種變體,憑借獨特的門控機制,在 ...
2025-07-07統(tǒng)計學(xué)方法在市場調(diào)研數(shù)據(jù)中的深度應(yīng)用? 市場調(diào)研是企業(yè)洞察市場動態(tài)、了解消費者需求的重要途徑,而統(tǒng)計學(xué)方法則是市場調(diào)研數(shù) ...
2025-07-07CDA數(shù)據(jù)分析師證書考試全攻略? 在數(shù)字化浪潮席卷全球的當(dāng)下,數(shù)據(jù)已成為企業(yè)決策、行業(yè)發(fā)展的核心驅(qū)動力,數(shù)據(jù)分析師也因此成為 ...
2025-07-07剖析 CDA 數(shù)據(jù)分析師考試題型:解鎖高效備考與答題策略? CDA(Certified Data Analyst)數(shù)據(jù)分析師考試作為衡量數(shù)據(jù)專業(yè)能力的 ...
2025-07-04SQL Server 字符串截取轉(zhuǎn)日期:解鎖數(shù)據(jù)處理的關(guān)鍵技能? 在數(shù)據(jù)處理與分析工作中,數(shù)據(jù)格式的規(guī)范性是保證后續(xù)分析準(zhǔn)確性的基礎(chǔ) ...
2025-07-04CDA 數(shù)據(jù)分析師視角:從數(shù)據(jù)迷霧中探尋商業(yè)真相? 在數(shù)字化浪潮席卷全球的今天,數(shù)據(jù)已成為企業(yè)決策的核心驅(qū)動力,CDA(Certifie ...
2025-07-04CDA 數(shù)據(jù)分析師:開啟數(shù)據(jù)職業(yè)發(fā)展新征程? ? 在數(shù)據(jù)成為核心生產(chǎn)要素的今天,數(shù)據(jù)分析師的職業(yè)價值愈發(fā)凸顯。CDA(Certified D ...
2025-07-03