码迷,mamicode.com
首页 > 编程语言 > 详细

python线程之condition

时间:2018-07-18 19:07:22      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:线程   lock   通知   tar   进程   first   bsp   指定   lease   

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

标签:线程   lock   通知   tar   进程   first   bsp   指定   lease   

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

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!