flowchart TD A["ev = threading.Event()"] --> B["is_set() is False"] B --> C["several threads call ev.wait()"] C --> D["all of them park -- no CPU used"] E["one thread calls ev.set()"] --> F["EVERY waiter is released"] F --> G["a thread arriving later does not block at all"] G --> H["wait() returns immediately while it stays set"] I["ev.clear()"] --> J["back to blocking for the next arrivals"] K["need to pass VALUES?"] --> L["use a Queue -- an Event carries no data"]
What is an Event?
An Event is a simple flag:
- one thread sets it
- other threads wait until it’s set
Example
import threading
import time
ready = threading.Event()
def waiter():
print("Waiting...")
ready.wait() # blocks
print("Go!")
def setter():
time.sleep(1)
ready.set()
t1 = threading.Thread(target=waiter)
t2 = threading.Thread(target=setter)
t1.start(); t2.start()
t1.join(); t2.join()Resetting an event
event.clear()resets it to false.- Useful for repeated cycles.
Check yourself
Three threads are blocked in `ev.wait()`. One thread calls `ev.set()`. How many are released?
Verified — all three logged 'released'. An Event is a latch on a shared flag, not a queue of permits.
Show answer
B — All three — Verified — all three logged 'released'. An Event is a latch on a shared flag, not a queue of permits.
A thread calls `wait()` on an Event that is ALREADY set. What happens?
Measured at 0.0000 s. An Event is level-triggered: it stays set, so late arrivals are not missed. `Condition.notify()` is edge-triggered and a late thread misses it entirely.
Show answer
B — It returns immediately — Measured at 0.0000 s. An Event is level-triggered: it stays set, so late arrivals are not missed. `Condition.notify()` is edge-triggered and a late thread misses it entirely.
When should you use a `Queue` instead of an `Event`?
An Event carries one bit and no payload. A Queue transfers items, blocks the consumer when empty, and applies back-pressure to the producer when full.
Show answer
B — When there is data to hand over — An Event carries one bit and no payload. A Queue transfers items, blocks the consumer when empty, and applies back-pressure to the producer when full.
What does `ev.clear()` do to threads currently blocked in `wait()`?
`clear()` only affects who blocks from then on. Threads released by an earlier `set()` have already moved on.
Show answer
B — Nothing — they were already released when it was set — `clear()` only affects who blocks from then on. Threads released by an earlier `set()` have already moved on.
🧪 Try It Yourself
Exercise 1 – threading.Event Set and Wait
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Clear an Event
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Event with Timeout
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.