Python asyncio synchronization: locks, events, and conditions
Answer: asyncio provides lock, event, condition, semaphore, and barrier primitives that look like threading primitives, but they are for tasks, not OS threads.
Thread safety and timeouts
asyncio synchronization primitives are designed to be similar to threading primitives.
These asyncio primitives are not safe across OS threads, so use the threading module when you need that kind of synchronization.
These primitives do not accept a timeout argument; use the wait_for function from asyncio to perform operations with timeouts.
The basic primitives are Lock, Event, Condition, Semaphore, BoundedSemaphore, and Barrier.
Lock, Event, and Condition
Lock implements a mutex for asyncio tasks and is not thread-safe.
Use a Lock when only one task should have exclusive access to a shared resource.
Prefer an async with statement for a Lock, which is equivalent to acquire and then release in a finally block.
acquire waits until the lock is unlocked, then takes it.
If several coroutines are blocked waiting for the same lock, the one that proceeds is the first that started waiting.
An Event notifies multiple tasks: it keeps an internal flag that starts clear, and wait blocks until another task sets the flag.
Once the event is set, wait returns immediately, and every waiting task is woken.
A Condition lets a task wait for some event and then take exclusive access to shared state.
It combines a lock and an event-style wait, and several Condition objects can share one Lock so different tasks can coordinate around particular states of the same resource.
See also
Claims
- id: C1 claim: asyncio synchronization primitives must not be used for OS thread synchronization. source: S1 evidence: "asyncio primitives are not thread-safe, therefore they should not"
- id: C2 claim: The preferred way to use a Lock is an async with statement. source: S1 evidence: "The preferred way to use a Lock is an async with statement"
- id: C3 claim: A Lock is a mutex for asyncio tasks and is not thread-safe. source: S2 evidence: "Implements a mutex lock for asyncio tasks. Not thread-safe."
Sources
- [S1] Synchronization primitives. Python Software Foundation. https://docs.python.org/3/library/asyncio-sync.html . License: PSF License Version 2. Accessed 2026-11-26.
- [S2] Synchronization primitives (source). CPython. https://raw.githubusercontent.com/python/cpython/main/Doc/library/asyncio-sync.rst . License: PSF License Version 2. Accessed 2026-11-26.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, Synchronization primitives; CPython, Synchronization primitives (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, Synchronization primitives; CPython, Synchronization primitives (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python asyncio synchronization: locks, events, and conditions." iinventedlight Library, iil:note:zg1490jh, version of 2026-11-26. https://iinventedlight.com/library/notes/python-asyncio-sync/. License: CC-BY-4.0.
@misc{iil_note_zg1490jh,
author = {{iinventedlight}},
title = {{Python asyncio synchronization: locks, events, and conditions}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-26},
url = {https://iinventedlight.com/library/notes/python-asyncio-sync/},
note = {Permanent ID iil:note:zg1490jh. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-asyncio-sync.schema.json (schema; CC-BY-4.0)
- python-asyncio-sync.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-asyncio-sync.md.