首页 > 编程语言 > 详细

python线程之condition

时间:2018-07-18 18:26:04      阅读:206      评论:0      收藏:0      [点我收藏+]

cond = threading.Condition()

 

# 类似lock.acquire()

cond.acquire()

 

# 类似lock.release()

cond.release()

 

# 等待指定触发,同时会释放对锁的获取,直到被notify才重新占有琐。

cond.wait()

 

# 发送指定,触发执行

cond.notify()

import threading,time

cond = 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

原文:https://www.cnblogs.com/saolv/p/9330740.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!