Events (signal between threads)
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?
Section titled “What is an Event?”An Event is a simple flag:
- one thread sets it
- other threads wait until it’s set
Example
Section titled “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
Section titled “Resetting an event”event.clear()resets it to false.- Useful for repeated cycles.
Check yourself
Section titled “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.
pch.quizShowAnswer
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.
pch.quizShowAnswer
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.
pch.quizShowAnswer
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.
pch.quizShowAnswer
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
Section titled “🧪 Try It Yourself”Exercise 1 – threading.Event Set and Wait
Section titled “Exercise 1 – threading.Event Set and Wait”Exercise 2 – Clear an Event
Section titled “Exercise 2 – Clear an Event”Exercise 3 – Event with Timeout
Section titled “Exercise 3 – Event with Timeout”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading