新聞中心
這里有您想知道的互聯(lián)網營銷解決方案
創(chuàng)新互聯(lián)Python教程:python中condition條件變量的作用
1、python提供的condition對象支持復雜的線程同步。

目前累計服務客戶上千多家,積累了豐富的產品開發(fā)及服務經驗。以網站設計水平和技術實力,樹立企業(yè)形象,為客戶提供網站制作、成都網站建設、網站策劃、網頁設計、網絡營銷、VI設計、網站改版、漏洞修補等服務。創(chuàng)新互聯(lián)公司始終以務實、誠信為根本,不斷創(chuàng)新和提高建站品質,通過對領先技術的掌握、對創(chuàng)意設計的研究、對客戶形象的視覺傳遞、對應用系統(tǒng)的結合,為客戶提供更好的一站式互聯(lián)網解決方案,攜手廣大客戶,共同發(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 + ': 我已經把眼睛蒙上了') 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 + ': 我已經藏好了,你快來找我吧') 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學習指路:創(chuàng)新互聯(lián)Python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
新聞名稱:創(chuàng)新互聯(lián)Python教程:python中condition條件變量的作用
本文網址:http://www.5511xx.com/article/cdcgehp.html


咨詢
建站咨詢
