最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python中使用Queue和Condition進(jìn)行線程同步的方法

 更新時(shí)間:2016年01月19日 17:17:10   作者:pizize  
這篇文章主要介紹了Python中使用Queue模塊和Condition對(duì)象進(jìn)行線程同步的方法,配合threading模塊下的線程編程進(jìn)行操作的實(shí)例,需要的朋友可以參考下

Queue模塊保持線程同步
利用Queue對(duì)象先進(jìn)先出的特性,將每個(gè)生產(chǎn)者的數(shù)據(jù)一次存入隊(duì)列,而每個(gè)消費(fèi)者將依次從隊(duì)列中取出數(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方法將線程名添加到隊(duì)列中
    print self.getName(),'put ',self.getName(),' to queue'
class Consumer(threading.Thread):# 定義消費(fèi)者類
  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方法獲取隊(duì)列中內(nèi)容
queue = Queue.Queue()  # 生成隊(duì)列對(duì)象
plist = []   # 生成者對(duì)象列表
clist = []   # 消費(fèi)者對(duì)象列表
for i in range(10):
  p = Producer('Producer' + str(i))
  plist.append(p)   # 添加到生產(chǎn)者對(duì)象列表
for i in range(10):
  c = Consumer('Consumer' + str(i))
  clist.append(c)   # 添加到消費(fèi)者對(duì)象列表
for i in plist:
  i.start()    # 運(yùn)行生產(chǎn)者線程
  i.join()
for i in clist:
  i.start()    # 運(yùn)行消費(fèi)者線程
  i.join()
######運(yùn)行結(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實(shí)現(xiàn)復(fù)雜的同步
使用Condition對(duì)象可以在某些事件觸發(fā)或者達(dá)到特定的條件后才處理數(shù)據(jù),Condition除了具有Lock對(duì)象的acquire方法和release方法外,
還有wait方法,notify方法,notifyAll方法等用于條件處理。
條件變量保持線程同步:threading.Condition()

  • wait():線程掛起,直到收到一個(gè)notify通知才會(huì)被喚醒繼續(xù)運(yùn)行
  • notify():通知其他線程,那些掛起的線程接到這個(gè)通知之后會(huì)開(kāi)始運(yùn)行
  • notifyAll(): 如果wait狀態(tài)線程比較多,notifyAll的作用就是通知所有線程(這個(gè)一般用得少)
#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() + ':一支穿云箭' #空白哥說(shuō)的第一句話
    self.cond.notify()          #喚醒其他wait狀態(tài)的線程(通知西米哥 讓他說(shuō)話)
    #然后進(jìn)入wait線程掛起狀態(tài)等待notify通知(等西米哥的回復(fù),接下來(lái)倆人就開(kāi)始扯蛋)
    self.cond.wait()
      
    print self.getName() + ':山無(wú)棱,天地合,乃敢與君絕!'
    self.cond.notify()
    self.cond.wait()
      
    print self.getName() + ':紫薇!?。?!(此處圖片省略)'
    self.cond.notify()
    self.cond.wait()
      
    print self.getName() + ':是你'
    self.cond.notify()
    self.cond.wait()
      
    #這里是空白哥說(shuō)的最后一段話,接下來(lái)就沒(méi)有對(duì)白了
    print self.getName() + ':有錢嗎 借點(diǎn)'
    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() +':千軍萬(wàn)馬來(lái)相見(jiàn)'
    self.cond.notify() #說(shuō)完話了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()
      
    #這里是最后一段話,后面空白哥沒(méi)接話了 所以說(shuō)完就釋放鎖 結(jié)束線程
    print self.getName() + ':滾' 
    self.cond.release()
      
      
kongbai = kongbaige(cond, '  ')
ximi = ximige(cond, '西米')
#尼瑪下面這2個(gè)啟動(dòng)標(biāo)志是關(guān)鍵,雖然是空白哥先開(kāi)的口,但是不能讓他先啟動(dòng),
#因?yàn)樗葐?dòng)的可能直到發(fā)完notify通知了,西米哥才開(kāi)始啟動(dòng),
#西米哥啟動(dòng)后會(huì)一直處于44行的wait狀態(tài),因?yàn)榭瞻赘缫呀?jīng)發(fā)完notify通知了進(jìn)入wait狀態(tài)了,
#而西米哥沒(méi)收到
#造成的結(jié)果就是2根線程就一直在那掛起,什么都不干,也不扯蛋了
ximi.start()
kongbai.start()

######運(yùn)行結(jié)果######

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

相關(guān)文章

  • np.where()[0] 和 np.where()[1]的具體使用

    np.where()[0] 和 np.where()[1]的具體使用

    這篇文章主要介紹了np.where()[0] 和 np.where()[1]的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • 一文帶你深入理解Python的`functools.lru_cache`裝飾器

    一文帶你深入理解Python的`functools.lru_cache`裝飾器

    Python中的functools.lru_cache裝飾器是一個(gè)非常有用的裝飾器,它可以幫助我們優(yōu)化遞歸函數(shù),避免重復(fù)計(jì)算已經(jīng)計(jì)算過(guò)的值,在這篇文章中,我們將探討?functools.lru_cache?的工作原理以及如何使用它,感興趣的朋友跟著小編一起來(lái)學(xué)習(xí)吧
    2023-07-07
  • Python模擬登陸實(shí)現(xiàn)代碼

    Python模擬登陸實(shí)現(xiàn)代碼

    本篇文章主要介紹了Python模擬登陸實(shí)現(xiàn)代碼,這里整理了詳細(xì)的代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • python通過(guò)TimedRotatingFileHandler按時(shí)間切割日志

    python通過(guò)TimedRotatingFileHandler按時(shí)間切割日志

    這篇文章主要介紹了python通過(guò)TimedRotatingFileHandler按時(shí)間切割日志的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-07-07
  • 基于Python實(shí)現(xiàn)PDF轉(zhuǎn)換文件格式

    基于Python實(shí)現(xiàn)PDF轉(zhuǎn)換文件格式

    這篇文章主要為大家詳細(xì)介紹了如何基于Python實(shí)現(xiàn)PDF轉(zhuǎn)換文件格式,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-01-01
  • python爬蟲(chóng)爬取某圖書(shū)網(wǎng)頁(yè)實(shí)例講解

    python爬蟲(chóng)爬取某圖書(shū)網(wǎng)頁(yè)實(shí)例講解

    這篇文章主要介紹了python爬蟲(chóng)爬取某圖書(shū)網(wǎng)頁(yè)實(shí)例,下面是通過(guò)requests庫(kù)來(lái)對(duì)ajax頁(yè)面進(jìn)行爬取的案例,與正常頁(yè)面不同,這里我們獲取url的方式也會(huì)不同,這里我們通過(guò)爬取一個(gè)簡(jiǎn)單的ajax小說(shuō)頁(yè)面來(lái)為大家講解,需要的朋友可以參考下
    2024-08-08
  • Python中斷點(diǎn)調(diào)試pdb包的用法詳解

    Python中斷點(diǎn)調(diào)試pdb包的用法詳解

    pdb(python debugger) 是 python 中的一個(gè)命令行調(diào)試包,為 python 程序提供了一種交互的源代碼調(diào)試功能,下面就跟隨小編一起學(xué)習(xí)一下它的具體使用吧
    2024-01-01
  • 用Python將Excel數(shù)據(jù)導(dǎo)入到SQL Server的例子

    用Python將Excel數(shù)據(jù)導(dǎo)入到SQL Server的例子

    今天小編就為大家分享一篇用Python將Excel數(shù)據(jù)導(dǎo)入到SQL Server的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-08-08
  • 最新評(píng)論

    三都| 巴林右旗| 兰州市| 庆云县| 湄潭县| 清原| 广灵县| 双桥区| 彰化县| 甘德县| 融水| 普陀区| 容城县| 白山市| 车致| 九寨沟县| 望江县| 黄陵县| 共和县| 突泉县| 大渡口区| 甘泉县| 广灵县| 安远县| 津南区| 河间市| 鄂州市| 涟源市| 全椒县| 城口县| 南陵县| 苍溪县| 枣强县| 泌阳县| 五家渠市| 汪清县| 郓城县| 静宁县| 黄石市| 浦县| 大渡口区|