← All notes

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

Sources

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

Permanent ID: iil:note:zg1490jh

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.},
}

Markdown

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.

All artifact files (JSON index)

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.