<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

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

        來源:懂視網 責編:小采 時間:2020-11-27 14:35:10
        文檔

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

        Python中使用Queue和Condition進行線程同步的方法:Queue模塊保持線程同步 利用Queue對象先進先出的特性,將每個生產者的數據一次存入隊列,而每個消費者將依次從隊列中取出數據 import threading # 導入threading模塊 import Queue # 導入Queue模塊 class Producer(threading
        推薦度:
        導讀Python中使用Queue和Condition進行線程同步的方法:Queue模塊保持線程同步 利用Queue對象先進先出的特性,將每個生產者的數據一次存入隊列,而每個消費者將依次從隊列中取出數據 import threading # 導入threading模塊 import Queue # 導入Queue模塊 class Producer(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 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實現復雜的同步
        使用Condition對象可以在某些事件觸發或者達到特定的條件后才處理數據,Condition除了具有Lock對象的acquire方法和release方法外,
        還有wait方法,notify方法,notifyAll方法等用于條件處理。
        條件變量保持線程同步:threading.Condition()

      1. wait():線程掛起,直到收到一個notify通知才會被喚醒繼續運行
      2. notify():通知其他線程,那些掛起的線程接到這個通知之后會開始運行
      3. notifyAll(): 如果wait狀態線程比較多,notifyAll的作用就是通知所有線程(這個一般用得少)
      4. #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狀態的線程(通知西米哥 讓他說話)
         #然后進入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()
        
        

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

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

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

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

        Python中使用Queue和Condition進行線程同步的方法:Queue模塊保持線程同步 利用Queue對象先進先出的特性,將每個生產者的數據一次存入隊列,而每個消費者將依次從隊列中取出數據 import threading # 導入threading模塊 import Queue # 導入Queue模塊 class Producer(threading
        推薦度:
        標簽: python 線程 condition
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 日韩av无码成人无码免费| 国产人成网在线播放VA免费| 免费A级毛片无码A∨中文字幕下载| 亚洲第一页日韩专区| 久久久久亚洲AV无码去区首| 日本午夜免费福利视频| 亚洲а∨天堂久久精品9966| 成人免费淫片在线费观看| 亚洲精品午夜国产va久久| 免费观看的a级毛片的网站| 亚洲码欧美码一区二区三区| 热99re久久免费视精品频软件 | 日本黄网站动漫视频免费| 亚洲精品熟女国产| 久久久高清免费视频| 亚洲国产精品无码久久| 免费一级毛片在线播放| 精品女同一区二区三区免费播放| 亚洲精品人成无码中文毛片| 香蕉免费在线视频| 亚洲日本视频在线观看| 四虎影视免费在线| 国产免费内射又粗又爽密桃视频| 亚洲成AV人片在线观看| 免费专区丝袜脚调教视频| 亚洲av无码一区二区三区人妖 | 国产精品亚洲综合| 亚洲第一福利网站在线观看| 亚洲第一区精品观看| 亚洲国产精品久久丫| 永久免费的网站在线观看| 妇女自拍偷自拍亚洲精品| 国产精品亚洲视频| **实干一级毛片aa免费| 亚洲AV无码专区在线电影成人| 亚洲无线一二三四区手机| 亚洲一区二区免费视频| 美景之屋4在线未删减免费| 久久亚洲国产伦理| 日本免费一区二区三区最新vr| 在线观看人成视频免费无遮挡|