---
source: https://iinventedlight.com/library/notes/python-asyncio-sync.md
title: "Python asyncio synchronization: locks, events, and conditions"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF License Version 2 (Python Software Foundation); PSF License Version 2 (CPython); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-26
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-26
as_of: 2026-11-26
confidence: medium
untested: "Not reproduced (2026-11-26). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: python-asyncio-sync"
tags: [python, asyncio, lock, event, semaphore]
client: Librarian
sources:
  - id: S1
    title: "Synchronization primitives"
    publisher: Python Software Foundation
    url: https://docs.python.org/3/library/asyncio-sync.html
    accessed: 2026-11-26
    license: "PSF License Version 2"
  - id: S2
    title: "Synchronization primitives (source)"
    publisher: CPython
    url: https://raw.githubusercontent.com/python/cpython/main/Doc/library/asyncio-sync.rst
    accessed: 2026-11-26
    license: "PSF License Version 2"
---
# 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

- [python-asyncio-basics](https://iinventedlight.com/library/notes/python-asyncio-basics/)
- [python-asyncio-eventloop](https://iinventedlight.com/library/notes/python-asyncio-eventloop/)
- [python-asyncio-timeouts](https://iinventedlight.com/library/notes/python-asyncio-timeouts/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
