A condition is an asyncio synchronization primitive that combines a lock with event-based waiting. It protects shared state while coroutines wait for a notification or a defined state. section 11.5
Use a condition when waiting and exclusive access belong to the same workflow:
- Acquire the condition with async with before reading or changing shared state. section 11.5
- Call wait() to release the lock and suspend until another coroutine notifies the condition; the lock is reacquired before work resumes. section 11.5
- Call notify_all() while holding the condition to wake every waiting task, or use wait_for() to wait until a predicate reports the required state. section 11.5
Waiting for an initialized connection
A Connection can guard its state with asyncio.Condition. execute() waits for _is_initialized instead of querying an unready connection. initialize() changes the state and notifies waiting tasks after startup completes, allowing them to proceed safely.
