asyncio shared/exclusive mode lock
Project description
asyncio-multilock
asyncio_multilock
provides MultiLock
, an asyncio
based lock with built-in
shared/exclusive mode semantics.
MultiLock.locked
can be in one of three states:
MultiLockType.NONE
- not acquired;MultiLockType.SHARED
- acquired one or more times in shared mode;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()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
asyncio_multilock-0.2.0.tar.gz
(20.3 kB
view details)
Built Distribution
File details
Details for the file asyncio_multilock-0.2.0.tar.gz
.
File metadata
- Download URL: asyncio_multilock-0.2.0.tar.gz
- Upload date:
- Size: 20.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 089403a6c979cf1fb1592c09987c3a2d3c59eb74a0baa7b445c6215fb994e18d |
|
MD5 | a4965a35211f077629d04f4cc5c3673d |
|
BLAKE2b-256 | 8422f5d82350aa2ce6dc3c215d1e85ef0ed8f9fd35052ccf596ae5b3d609a221 |
File details
Details for the file asyncio_multilock-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: asyncio_multilock-0.2.0-py3-none-any.whl
- Upload date:
- Size: 3.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b5e576050afb8a0a687a8d0dcc49cf2266cd8c809bd70e3a0c64d48d8e664294 |
|
MD5 | 614ba9f12f3fc77222bab193aa255886 |
|
BLAKE2b-256 | 76356a367801031c4b00ae60ab16089b520287b85eacd56d274050aa7b05188e |