![](/img/trans.png)
[英]When to use event/condition/lock/semaphore in python's threading module?
[英]What's the difference of Event and Lock in python threading module?
Event
和Lock
在这些场景中是否做同样的事情?
class MyThread1(threading.Thread):
def __init__(event):
self.event = event
def run(self):
self.event.wait()
# do something
self.event.clear()
另一个:
class MyThread2(threading.Thread):
def __init__(lock):
self.lock = lock
def run(self):
self.lock.acquire()
# do something
self.lock.release()
如果等待某个事件,执行将停止,直到发生event.set()
event.wait() # waits for event.set()
如果已经获得锁定,则仅获取锁定会停止
lock.acquire() # first time: returns true
lock.acquire() # second time: stalls until lock.release()
这两个类都有不同的用例。 本文将帮助您了解这些差异。
实际上,我发现python中Event与Lock之间的区别是:
可能还会有更多的差异,但上面对我来说是最明显的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.