Python 死锁现象
Posted 缥缈映苍穹
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 死锁现象相关的知识,希望对你有一定的参考价值。
import time from threading import Thread,Lock,RLock def f1(locA,locB): locA.acquire() print(‘f1>>1号抢到了A锁‘) time.sleep(1) locB.acquire() print(‘f1>>1号抢到了B锁‘) locB.release() locA.release() def f2(locA,locB): locB.acquire() print(‘f2>>2号抢到了B锁‘) locA.acquire() time.sleep(1) print(‘f2>>2号抢到了A锁‘) locA.release() locB.release() if __name__ == ‘__main__‘: locA = Lock() locB = Lock() t1 = Thread(target=f1,args=(locA,locB)) t2 = Thread(target=f2,args=(locA,locB)) t1.start() t2.start()
以上是关于Python 死锁现象的主要内容,如果未能解决你的问题,请参考以下文章
python并发编程之线程(创建线程,锁(死锁现象,递归锁),GIL锁)