What is the difference in event and lock in python stream module?

Do Event and Lock tags Lock in these scenes?

 class MyThread1(threading.Thread): def __init__(event): self.event = event def run(self): self.event.wait() # do something self.event.clear() 

other:

 class MyThread2(threading.Thread): def __init__(lock): self.lock = lock def run(self): self.lock.acquire() # do something self.lock.release() 
+6
source share
2 answers

If you are waiting for an event, execution stops until event.set() occurs

 event.wait() # waits for event.set() 

Acquiring a lock only kiosks, if the lock is already purchased

 lock.acquire() # first time: returns true lock.acquire() # second time: stalls until lock.release() 

Both classes have different use cases. This article will help you understand the differences.

+5
source

Practically speaking, I found the difference between Event and Lock in python:

  • An event can have many waiters, and when the event is set or started, ALL these waiters will wake up.
  • A lock can have many waiters, and when the lock is released, only one waiter wakes up and as a result gets a lock.

There may still be more differences, but this is the most obvious to me.

+1
source

Source: https://habr.com/ru/post/920291/


All Articles