Skip to main content

asyncio-multilock

License GitHub Actions Workflow Status

asyncio_multilock provides MultiLock, an asyncio based lock with built-in shared/exclusive mode semantics.

MultiLock.locked can be in one of three states:

  1. MultiLockType.NONE - not acquired;
  2. MultiLockType.SHARED - acquired one or more times in shared mode;
  3. MultiLockType.EXCLUSIVE - acquired one time in exclusive mode.

When a lock is acquired, a Hashable handle is returned which uniquely identifies the acquisition. This handle is used to release the acquisition.

from asyncio import create_task, sleep
from asyncio_multilock import MultiLock, MultiLockType

lock = MultiLock()
assert not lock.locked

shared1 = await lock.acquire(MultiLockType.SHARED)
assert lock.locked is MultiLockType.SHARED

shared2 = await lock.acquire(MultiLockType.SHARED)

async def wait_release_shared(delay: float) -> None:
    await sleep(delay)
    lock.release(shared1)
    lock.release(shared2)
create_task(wait_release_shared(3))

# Acquisition context manager.
async with lock.context(MultiLockType.EXCLUSIVE) as exclusive:
    # Blocked for 3 seconds.
    assert lock.locked is MultiLockType.EXCLUSIVE

The lock can also be acquired synchronously, returning no handle if the acquisition fails.

from asyncio_multilock import MultiLock, MultiLockType

lock = MultiLock()
assert not lock.locked

shared = lock.acquire_nowait(MultiLockType.SHARED)
assert shared

exclusive = lock.acquire_nowait(MultiLockType.EXCLUSIVE)
assert not exclusive

assert lock.locked is MultiLockType.SHARED

The lock can also be monitored for when a given lock type is next acquirable.

from asyncio_multilock import MultiLock, MultiLockType

lock = MultiLock()

async with lock.notify(MultiLockType.SHARED) as event:
    await event.wait()
    print("shared lock is acquirable")
    event.clear()

Metadata

Release files for asyncio-multilock 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for asyncio-multilock 0.2.0
File Size Uploaded
asyncio_multilock-0.2.0.tar.gz 20.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for asyncio-multilock 0.2.0
File Interpreter ABI Platform
asyncio_multilock-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 24.0 kB

Release files / asyncio_multilock-0.2.0.tar.gz

Download URL asyncio_multilock-0.2.0.tar.gz
Size 20.3 kB
Tags Source
SHA-256 checksum
How to use checksums
089403a6c979cf1fb1592c09987c3a2d3c59eb74a0baa7b445c6215fb994e18d
BLAKE2b-256 checksum
How to use checksums
8422f5d82350aa2ce6dc3c215d1e85ef0ed8f9fd35052ccf596ae5b3d609a221
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.5

Release files / asyncio_multilock-0.2.0-py3-none-any.whl

Download URL asyncio_multilock-0.2.0-py3-none-any.whl
Size 3.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b5e576050afb8a0a687a8d0dcc49cf2266cd8c809bd70e3a0c64d48d8e664294
BLAKE2b-256 checksum
How to use checksums
76356a367801031c4b00ae60ab16089b520287b85eacd56d274050aa7b05188e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.5

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page