Asyncio Tokenized Lock
Asyncio Tokenized Lock is a Python library that provides a token-based locking mechanism for managing asynchronous locks in an asyncio environment. It introduces two main classes, LockManager and TokenizedLock, to facilitate the coordination of asynchronous tasks with associated tokens.
Features
- Tokenized Locking: Locks are associated with tokens, allowing for fine-grained control over asynchronous operations.
- Asynchronous Support: Built on asyncio, enabling efficient coordination of asynchronous tasks.
- Context Manager Interface:
TokenizedLockcan be used as an asynchronous context manager for clean and concise lock management. - Timeout Support: The library supports timeouts for lock acquisition.
Installation
pip install asyncio-tokenized-lock
Usage (Examples)
Basic Usage
from asyncio_tokenized_lock.lock import LockManager, TokenizedLock
manager = LockManager[str]()
lock = manager.register("example_token")
# Acquire and release the lock
async with lock:
# Perform operations while holding the lock
pass
Timeout
Context Manager
from asyncio_tokenized_lock.lock import LockManager, TokenizedLock
manager = LockManager[str]()
token = "example_token"
lock = manager.register(token)
lock.ctx_timeout = 1.0 # Set the timeout to 1 second
try:
# Acquire the lock with a timeout using context manager
async with lock:
# Perform operations while holding the lock
pass
except asyncio.TimeoutError:
...
Inline
try:
await lock.acquire(timeout=1.0)
# Perform operations while holding the lock
except asyncio.TimeoutError:
print("Lock acquisition timed out")
Queue Concurrency
import asyncio
import uuid
import logging
from asyncio_tokenized_lock.lock import LockManager, TokenizedLock
log = logging.getLogger("my-module")
async def consume_queue_safely(concurrency: int = 5, queue_size: int = 100):
manager = LockManager[str]()
queue = asyncio.Queue()
put_tasks = [
asyncio.ensure_future(queue.put(item=uuid.uuid4()))
for _ in range(queue_size)
]
async def safe_consume(queue: asyncio.Queue):
while not queue.empty():
item = await queue.get()
lock = manager.register(item)
if lock.locked:
continue
async with lock:
# Perform operations with the locked item
yield item
# Worker class to consume from the queue safely
@dataclass
class Worker:
id: str
queue: asyncio.Queue
async def consume(self):
while True:
async for item in safe_consume(self.queue):
# Perform operations with the item
log.info(f"[WORKER-{self.id}] Item {item} processed")
break
workers = [Worker(id=str(i), queue=queue) for i in range(concurrency)]
consume_tasks = [asyncio.ensure_future(w.consume()) for w in workers]
# Wait for tasks to complete
await asyncio.wait(put_tasks)
await asyncio.wait(consume_tasks)
Testing
poetry run pytest
Run the provided test suite to ensure the correct behavior of the LockManager and TokenizedLock classes in different scenarios.
Contributing
Contributions are welcome! Feel free to open issues, submit pull requests, or suggest improvements.
License
This library is licensed under the MIT License - see the LICENSE file for details.
Metadata
Release files for asyncio-tokenized-lock 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| asyncio_tokenized_lock-0.1.3.tar.gz | 5.6 kB | Without provenance | Details for asyncio_tokenized_lock-0.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| asyncio_tokenized_lock-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for asyncio_tokenized_lock-0.1.3-py3-none-any.whl |
Total release size: 11.0 kB
Release files / asyncio_tokenized_lock-0.1.3.tar.gz
| Download URL | asyncio_tokenized_lock-0.1.3.tar.gz |
|---|---|
| Size | 5.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7649cbf304551dafa5069b5a172995901ec439de1479d3842c7a316c87f97b75
|
|
BLAKE2b-256 checksum How to use checksums |
eca6dff2244b8aa82101480833eaa066420e7ddcd06c70b95bd388541e668799
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.10.13 Linux/6.2.0-1016-azure
|
Release files / asyncio_tokenized_lock-0.1.3-py3-none-any.whl
| Download URL | asyncio_tokenized_lock-0.1.3-py3-none-any.whl |
|---|---|
| Size | 5.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a3c36a6a60f73bcc54282c39b44e4a4b12348693e3a03253281bc70362494265
|
|
BLAKE2b-256 checksum How to use checksums |
a00cd4cb17e3560863d3e1a5abe20b6eef4c2f58b3cc82d73e9737b80f25cd95
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.1 CPython/3.10.13 Linux/6.2.0-1016-azure
|
Security for asyncio-tokenized-lock 0.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.