国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python中使用Queue和Condition進行線程同步的方法

2020-01-04 17:50:10
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了Python中使用Queue模塊和Condition對象進行線程同步的方法,配合threading模塊下的線程編程進行操作的實例,需要的朋友可以參考下
 

Queue模塊保持線程同步
利用Queue對象先進先出的特性,將每個生產者的數據一次存入隊列,而每個消費者將依次從隊列中取出數據

import threading    # 導入threading模塊import Queue      # 導入Queue模塊class Producer(threading.Thread):# 定義生產者類  def __init__(self,threadname):    threading.Thread.__init__(self,name = threadname)  def run(self):    global queue  # 聲明queue為全局變量    queue.put(self.getName())  # 調用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'#調用get方法獲取隊列中內容queue = Queue.Queue()  # 生成隊列對象plist = []   # 生成者對象列表clist = []   # 消費者對象列表for i in range(10):  p = Producer('Producer' + str(i))  plist.append(p)   # 添加到生產者對象列表for i in range(10):  c = Consumer('Consumer' + str(i))  clist.append(c)   # 添加到消費者對象列表for i in plist:  i.start()    # 運行生產者線程  i.join()for i in clist:  i.start()    # 運行消費者線程  i.join()######運行結果######>>> Producer0 put Producer0 to queueProducer1 put Producer1 to queueProducer2 put Producer2 to queueProducer3 put Producer3 to queueProducer4 put Producer4 to queueProducer5 put Producer5 to queueProducer6 put Producer6 to queueProducer7 put Producer7 to queueProducer8 put Producer8 to queueProducer9 put Producer9 to queueConsumer0 get Producer0 from queueConsumer1 get Producer1 from queueConsumer2 get Producer2 from queueConsumer3 get Producer3 from queueConsumer4 get Producer4 from queueConsumer5 get Producer5 from queueConsumer6 get Producer6 from queueConsumer7 get Producer7 from queueConsumer8 get Producer8 from queueConsumer9 get Producer9 from queue

Condition實現復雜的同步
使用Condition對象可以在某些事件觸發或者達到特定的條件后才處理數據,Condition除了具有Lock對象的acquire方法和release方法外,
還有wait方法,notify方法,notifyAll方法等用于條件處理。
條件變量保持線程同步:threading.Condition()

  • wait():線程掛起,直到收到一個notify通知才會被喚醒繼續運行
  • notify():通知其他線程,那些掛起的線程接到這個通知之后會開始運行
  • notifyAll(): 如果wait狀態線程比較多,notifyAll的作用就是通知所有線程(這個一般用得少)
#coding:utf-8import threadingimport timecond = 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狀態的線程(通知西米哥 讓他說話)    #然后進入wait線程掛起狀態等待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.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()          #這里是最后一段話,后面空白哥沒接話了 所以說完就釋放鎖 結束線程    print self.getName() + ':滾'     self.cond.release()            kongbai = kongbaige(cond, '  ')ximi = ximige(cond, '西米')#尼瑪下面這2個啟動標志是關鍵,雖然是空白哥先開的口,但是不能讓他先啟動,#因為他先啟動的可能直到發完notify通知了,西米哥才開始啟動,#西米哥啟動后會一直處于44行的wait狀態,因為空白哥已經發完notify通知了進入wait狀態了,#而西米哥沒收到#造成的結果就是2根線程就一直在那掛起,什么都不干,也不扯蛋了ximi.start()kongbai.start()

######運行結果######

  :一支穿云箭西米:千軍萬馬來相見  :山無棱,天地合,乃敢與君絕!西米:海可枯,石可爛,激情永不散!  :紫薇!!!!(此處圖片省略)西米:爾康!!!(此處圖片省略)  :是你西米:是我  :有錢嗎 借點西米:滾

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宽甸| 吉首市| 子洲县| 彭山县| 永春县| 宿州市| 凤城市| 洛阳市| 平湖市| 南皮县| 永平县| 邯郸市| 大关县| 石狮市| 涡阳县| 仁化县| 建瓯市| 堆龙德庆县| 江源县| 福清市| 屏山县| 达日县| 曲阳县| 肇州县| 昆山市| 清原| 调兵山市| 河源市| 邹平县| 永安市| 嵊州市| 东兴市| 万州区| 义乌市| 河西区| 斗六市| 方城县| 广饶县| 黔南| 丰台区| 电白县|