python多線程--theading模塊

使用Condition對象可以在某些事件觸發或者達到特定的條件後才處理數據,Condition除了具有Lock對象的acquire方法和release方法外,

還有wait方法,notify方法,notifyAll方法等用於條件處理。

條件變量保持線程同步:threading.Condition()

wait():線程掛起,直到收到一個notify通知纔會被喚醒繼續運行

notify():通知其他線程,那些掛起的線程接到這個通知之後會開始運行

notifyAll(): 如果wait狀態線程比較多,notifyAll的作用就是通知所有線程(這個一般用得少)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PS:有朋友說看了我日誌裏面的代碼也看了註釋,但是還是不懂,那今天來個簡單的,
用一對屌絲(空白哥和西米哥)的對話來詮釋代碼(生產者-消費者)原圖:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#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()
######運行結果######
    :一支穿雲箭
西米:千軍萬馬來相見
    :山無棱,天地合,乃敢與君絕!
西米:海可枯,石可爛,激情永不散!
    :紫薇!!!!(此處圖片省略)
西米:爾康!!!(此處圖片省略)
    :是你
西米:是我
    :有錢嗎 借點
西米:滾

轉自:http://zeping.blog.51cto.com/6140112/1258977

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章