新聞中心
1、python提供的condition對象支持復(fù)雜的線程同步。

目前累計服務(wù)客戶上千多家,積累了豐富的產(chǎn)品開發(fā)及服務(wù)經(jīng)驗。以網(wǎng)站設(shè)計水平和技術(shù)實力,樹立企業(yè)形象,為客戶提供網(wǎng)站制作、成都網(wǎng)站建設(shè)、網(wǎng)站策劃、網(wǎng)頁設(shè)計、網(wǎng)絡(luò)營銷、VI設(shè)計、網(wǎng)站改版、漏洞修補等服務(wù)。創(chuàng)新互聯(lián)公司始終以務(wù)實、誠信為根本,不斷創(chuàng)新和提高建站品質(zhì),通過對領(lǐng)先技術(shù)的掌握、對創(chuàng)意設(shè)計的研究、對客戶形象的視覺傳遞、對應(yīng)用系統(tǒng)的結(jié)合,為客戶提供更好的一站式互聯(lián)網(wǎng)解決方案,攜手廣大客戶,共同發(fā)展進步。
2、Condition被稱為條件變量,除了提供類似Lock的acquire和release方法外,還提供wait和notify方法。線程先acquire條件變量,然后判斷一些條件。
實例
import threading, time class Hider(threading.Thread): def __init__(self, cond, name): super(Hider, self).__init__() self.cond = cond self.name = name def run(self): time.sleep(1) #確保先運行Seeker中的方法 self.cond.acquire() #b print(self.name + ': 我已經(jīng)把眼睛蒙上了') self.cond.notify() self.cond.wait() #c #f print(self.name + ': 我找到你了 ~_~') # self.cond.notify() self.cond.release() #g print(self.name + ': 我贏了') #h class Seeker(threading.Thread): def __init__(self, cond, name): super(Seeker, self).__init__() self.cond = cond self.name = name def run(self): self.cond.acquire() self.cond.wait() #a #釋放對瑣的占用,同時線程掛起在這里,直到被notify并重新占有瑣。 #d print(self.name + ': 我已經(jīng)藏好了,你快來找我吧') self.cond.notify() self.cond.wait() #e #h self.cond.release() print(self.name + ': 被你找到了,哎~~~') cond = threading.Condition() seeker = Seeker(cond, 'seeker') hider = Hider(cond, 'hider') seeker.start() hider.start()
以上就是python中condition條件變量的作用,希望對大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)Python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
網(wǎng)站名稱:創(chuàng)新互聯(lián)Python教程:python中condition條件變量的作用
網(wǎng)站URL:http://fisionsoft.com.cn/article/cdcgehp.html


咨詢
建站咨詢
