python多線程(條件變量同步)

 

互斥鎖是最簡單的線程同步機制,Python提供的Condition對象提供了對複雜線程同步問題的支持。Condition被稱爲條件變量,除了提供與Lock相似的acquire和release方法外,還提供了wait和notify方法。線程首先acquire一個條件變量,而後判斷一些條件。若是條件不知足則wait;若是條件知足,進行一些處理改變條件後,經過notify方法通知其餘線程,其餘處於wait狀態的線程接到通知後會從新判斷條件。不斷的重複這一過程,從而解決複雜的同步問題。 html

能夠認爲Condition對象維護了一個鎖(Lock/RLock)和一個waiting池。線程經過acquire得到Condition對象,當調用wait方法時,線程會釋放Condition內部的鎖並進入blocked狀態,同時在waiting池中記錄這個線程。當調用notify方法時,Condition對象會從waiting池中挑選一個線程,通知其調用acquire方法嘗試取到鎖。 python

Condition對象的構造函數能夠接受一個Lock/RLock對象做爲參數,若是沒有指定,則Condition對象會在內部自行建立一個RLock。 函數

除了notify方法外,Condition對象還提供了notifyAll方法,能夠通知waiting池中的全部線程嘗試acquire內部鎖。因爲上述機制,處於waiting狀態的線程只能經過notify方法喚醒,因此notifyAll的做用在於防止有線程永遠處於沉默狀態。 ui

演示條件變量同步的經典問題是生產者與消費者問題:假設有一羣生產者(Producer)和一羣消費者(Consumer)經過一個市場來交互產品。生產者的」策略「是若是市場上剩餘的產品少於1000個,那麼就生產100個產品放到市場上;而消費者的」策略「是若是市場上剩餘產品的數量多餘100個,那麼就消費3個產品。用Condition解決生產者與消費者問題的代碼以下: 線程


import threading code

import time class Producer(threading.Thread): def run(self): global count while True: if con.acquire(): if count > 1000: con.wait() else: count = count+100 msg = self.name+' produce 100, count=' + str(count) print msg con.notify() con.release() time.sleep(1) class Consumer(threading.Thread): def run(self): global count while True: if con.acquire(): if count < 100: con.wait() else: count = count-3 msg = self.name+' consume 3, count='+str(count) print msg con.notify() con.release() time.sleep(1) count = 500 con = threading.Condition() def test(): for i in range(2): p = Producer() p.start() for i in range(5): c = Consumer() c.start() if __name__ == '__main__': test()
相關文章
相關標籤/搜索