python之递归锁Rlock
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python之递归锁Rlock相关的知识,希望对你有一定的参考价值。
# 递归锁:就是一把锁中还有一把小锁,比如学校的大门口有一个大锁,学校里的 #每个教室也有一把小锁,以后所有的锁都用rlock就可以了,不要用lock,尤其是多层锁的时候,必须要用递归锁 import threading import time def run1(): print("grab the first part data") lock.acquire() global num1 num1 += 1 lock.release() return num1 def run2(): print("grab the second part data") lock.acquire() global num2 num2 += 1 lock.release() return num2 def run3(): lock.acquire() res1 = run1() print("--------between run1 and run2-------") res2 = run2() lock.release() print("函数run1--->",res1,"函数run2--->",res2) if __name__ == ‘__main__‘: num1 = 0 num2 = 0 lock = threading.RLock() for i in range(5): t = threading.Thread(target=run3) t.start() while threading.active_count() != 1: print("剩余的线程数;[%s]" %threading.active_count()) else: print("all thread is down") print(num1,num2)
以上是关于python之递归锁Rlock的主要内容,如果未能解决你的问题,请参考以下文章
python并发编程之多线程2------------死锁与递归锁,信号量等