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()
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)
| File | Size | Uploaded | |
|---|---|---|---|
| asyncio_multilock-0.2.0.tar.gz | 20.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| 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
|