日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:Python如何實(shí)現(xiàn)條件變量同步

察布查爾錫伯網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、APP開(kāi)發(fā)、響應(yīng)式網(wǎng)站等網(wǎng)站項(xiàng)目制作,到程序開(kāi)發(fā),運(yùn)營(yíng)維護(hù)。創(chuàng)新互聯(lián)2013年至今到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。

條件變量同步

有一類線程需要滿足條件之后才能夠繼續(xù)執(zhí)行,python提供了threading.Condition 對(duì)象用于條件變量線程的支持,它除了能提供RLock()或Lock()的方法外,還提供了 wait()、notify()、notifyAll()方法。

lock_con=threading.Condition([Lock/Rlock]): 鎖是可選選項(xiàng),不傳人鎖,對(duì)象自動(dòng)創(chuàng)建一個(gè)RLock()。

wait():條件不滿足時(shí)調(diào)用,線程會(huì)釋放鎖并進(jìn)入等待阻塞;

notify():條件創(chuàng)造后調(diào)用,通知等待池激活一個(gè)線程;

notifyAll():條件創(chuàng)造后調(diào)用,通知等待池激活所有線程。

相關(guān)推薦:《Python視頻教程》

import threading, time
from random import randint
class Producer(threading.Thread):
    def run(self):
        global L
        while True:
            val = randint(0, 100)
            print('生產(chǎn)者', self.name, ':Append'+str(val),L)
            if lock_con.acquire():
                L.append(val)
                lock_con.notify()
                lock_con.release()
            time.sleep(3)
class Consumer(threading.Thread):
    def run(self):
        global L
        while True:
            lock_con.acquire()
            if len(L) == 0:
                lock_con.wait()
            print('消費(fèi)者', self.name, ":Delete" + str(L[0]), L)
            del L[0]
            lock_con.release()
            time.sleep(0.25)
if __name__ == "__main__":
    L = []
    lock_con = threading.Condition()
    threads = []
    for i in range(5):
        threads.append(Producer())
    threads.append(Consumer())
    for t in threads:
        t.start()
    for t in threads:
        t.join()
    print('---- end ----')

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

生產(chǎn)者 Thread-1 :Append63 []
生產(chǎn)者 Thread-2 :Append66 [63]
生產(chǎn)者 Thread-3 :Append20 [63, 66]
生產(chǎn)者 Thread-4 :Append83 [63, 66, 20]
生產(chǎn)者 Thread-5 :Append2 [63, 66, 20, 83]
生產(chǎn)者 Thread-4 :Append26 []
消費(fèi)者 Thread-6 :Delete26 [26]
生產(chǎn)者 Thread-2 :Append21 []
生產(chǎn)者 Thread-3 :Append71 [21]
生產(chǎn)者 Thread-1 :Append19 [21, 71]
生產(chǎn)者 Thread-5 :Append100 [21, 71, 19]
生產(chǎn)者 Thread-1 :Append96 []
消費(fèi)者 Thread-6 :Delete96 [96]
........

文章標(biāo)題:創(chuàng)新互聯(lián)Python教程:Python如何實(shí)現(xiàn)條件變量同步
當(dāng)前地址:http://www.5511xx.com/article/dhecoji.html