Course contentsShow
← Python
Lesson 867 of 1,06014. Concurrency and PerformanceFree lesson

Signaling State Changes Using Condition Variables

Coordinate complex thread interactions by allowing threads to wait for specific state changes signaled by other worker threads.

In this lesson, you will learn how to orchestrate threads by having them pause execution until specific application conditions are met, rather than relying on inefficient polling.

The Problem with Busy-Waiting

When working with shared resources in your data pipelines, you often encounter scenarios where a worker thread cannot proceed until another thread completes a specific task—such as finishing an API fetch or finalizing a data transformation. Using simple Lock objects forces you into "busy-waiting" (a while loop that constantly checks a flag), which wastes CPU cycles.

The Solution: Condition Variables

A threading.Condition acts as a specialized monitor that allows threads to wait for a state change. It internally contains a Lock, but it adds the ability to wait() and notify().

When a thread calls wait(), it automatically releases the underlying lock and goes to sleep. This is crucial because it allows other threads to acquire the lock and perform the work necessary to change the state. Once the worker thread finishes, it calls notify() (or notify_all()), which wakes up the waiting thread. Upon waking, the thread automatically re-acquires the lock, ensuring it safely resumes execution in a synchronized environment.

Code Example: Coordinating a Data Processor

Imagine a thread waiting for a data buffer to be populated:

import threading

condition = threading.Condition()
data_buffer = []

def consumer():
    with condition:
        while not data_buffer:
            condition.wait()  # Thread sleeps until notified
        print(f"Processing data: {data_buffer.pop(0)}")

def producer():
    with condition:
        data_buffer.append("New Payload")
        condition.notify()  # Wakes the consumer

By using a while loop inside the with block (while not data_buffer:), we guard against "spurious wakeups"—a phenomenon where a thread might wake up even if the condition hasn't been met. The condition variable ensures that your threads transition from a blocked state to active execution only when the signal arrives, making your concurrent architecture significantly more efficient.

Key Takeaway: Condition variables provide a robust signaling mechanism that lets threads sleep efficiently while waiting for state changes, replacing expensive busy-waiting with event-driven coordination.