python線程之condition

來源:互聯網
上載者:User

標籤:線程   lock   通知   tar   進程   first   bsp   指定   lease   

cond = threading.Condition()

 

# 類似lock.acquire()

cond.acquire()

 

# 類似lock.release()

cond.release()

 

# 等待指定觸發,同時會釋放對鎖的擷取,直到被notify才重新佔有瑣。

cond.wait()

 

# 發送指定,觸發執行

cond.notify()

import threading,timecond = threading.Condition()#cond.acquire()#cond.release()#cond.wait()#cond.notify()def aa():    print (‘thread_aa get‘)    cond.acquire()    time.sleep(5)    print (‘thread_aa finished first job‘)    cond.wait()#釋放對鎖的控制,好讓別的進程acquire到               #同時阻塞在此,等待得到鎖的那個進程的先cond.notify後(cond.wait或cond.release)    print (‘thread_aa get again‘)    time.sleep(5)    print (‘thread_aa finished all work‘)    cond.notify()#告訴剛才釋放鎖的那個進程等通知吧,等我要麼wait要麼release    cond.release()#釋放鎖def bb():    cond.acquire()    print (‘thread_bb get‘)    cond.notify()#告訴剛才釋放鎖的那個進程等通知吧,等我要麼wait要麼release    print (‘依然是我bb在運行‘)    time.sleep(5)    print (‘thread_bb finished first job‘)    cond.wait()#釋放對鎖的控制,而後被阻塞的aa就可以執行了            #同時阻塞在此,等待得到鎖的aa的先cond.notify後(cond.wait或cond.release)    print (‘thread_aa get again‘)        print (‘thread_bb finished all works‘)    cond.release()    a=threading.Thread(target=aa)a.start()time.sleep(2)b=threading.Thread(target=bb)b.start()

 

python線程之condition

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.