Python 中的Lock与RLock
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import threading count = 0 def print_time(threadName): global count c = 0 while (c< 100 ): c + = 1 count + = 1 print ( "{0}:set count to {1}" . format (threadName,count)) try : threading.Thread( target = print_time, args = ( "Thread-1" , ) ).start() threading.Thread( target = print_time, args = ( "Thread-2" , ) ).start() threading.Thread( target = print_time, args = ( "Thread-3" , ) ).start() except Exception as e: print ( "Error: unable to start thread" )<br><br> |
结果“ 每个thread对count进行改动期间while(c<100)
,有其它的thread插入进来改动count
通过threading.Lock() 实现,通过Lock的acquire()和release()函数
来控制加锁和解锁,使用简单得方法 with实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import threading lock = threading.Lock() count = 0 def print_time(threadName): global count c = 0 with lock: while (c< 100 ): c + = 1 count + = 1 print ( "{0}:set count to {1}" . format (threadName,count)) try : threading.Thread( target = print_time, args = ( "Thread-1" , ) ).start() threading.Thread( target = print_time, args = ( "Thread-2" , ) ).start() threading.Thread( target = print_time, args = ( "Thread-3" , ) ).start() except Exception as e: print ( "Error: unable to start thread" ) |
结果
通过threading.Rlock() 实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import threading rlock = threading.RLock() count = 0 def print_time(threadName): global count c = 0 with rlock: while (c< 100 ): c + = 1 count + = 1 print ( "{0}:set count to {1}" . format (threadName,count)) try : threading.Thread( target = print_time, args = ( "Thread-1" , ) ).start() threading.Thread( target = print_time, args = ( "Thread-2" , ) ).start() threading.Thread( target = print_time, args = ( "Thread-3" , ) ).start() except Exception as e: print ( "Error: unable to start thread" ) |
j结果
Lock和Rlock的区别
1 2 3 4 5 6 | import threading lock = threading.Lock() #Lock对象 lock.acquire() lock.acquire() #产生了死琐。 lock.release() lock.release() |
1 2 3 4 5 6 | import threading lock = threading.RLock() #Lock对象 lock.acquire() lock.acquire() 程序不会堵塞 lock.release() lock.release() |
Locks | RLocks |
---|---|
A Lock object can not be acquired again by any thread unless it is released by the thread which which is accessing the shared resource. 一个lock被释放前不能被其他线程获得acquire |
An RLock object can be acquired numerous times by any thread. 一个Rlock可以被其他任意线程获得 |
A Lock object can be released by any thread. 一个lock可以被其他线程释放 |
An RLock object can only be released by the thread which acquired it. Rlock只能被获得他的线程释放 |
A Lock object can be owned by one lock被一个线程占有 |
An RLock object can be owned by many threads Rlock可以被其他线程获得 |
Execution of a Lock object is faster. lock的运行速度快 |
Execution of an RLock object is slower than a Lock object 运行速度比lock慢 |
这两种琐的主要区别是:RLock允许在同一线程中被多次acquire。而Lock却不允许这种情况
。注意:如果使用RLock
,那么acquire和release必须成对出现
,即调用了n次acquire,必须调用n次的release才能真正释放所占用的琐
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· winform 绘制太阳,地球,月球 运作规律
· 上周热点回顾(3.3-3.9)
2020-04-09 k8s的图片