leasedkeyq
Async Keyed Leased Queue - An asyncio-friendly in-memory queue combining FIFO semantics, dictionary-style keyed access, and lease-based exclusive processing.
Features
- FIFO Queue Semantics: Process items in order with
get() - Keyed Access: Target specific items with
take(key) - Lease-Based Processing: Exclusive access with explicit
ack/release - O(1) Operations: Constant-time enqueue, dequeue, and key-based removal
- Automatic Timeouts: Optional lease expiration with auto-retry
- Blocking Behavior: Async waiting for items or specific keys
- Type Safe: Full type hints with strict mypy compliance
- Zero Dependencies: Pure Python stdlib implementation
Installation
pip install leasedkeyq
Quick Start
import asyncio
from leasedkeyq import LeasedKeyQueue
async def main():
# Create queue with 30-second lease timeout
async with LeasedKeyQueue[str, dict](default_lease_timeout=30.0) as queue:
# Producer: add items
await queue.put("task-1", {"action": "process", "data": 123})
await queue.put("task-2", {"action": "send", "data": 456})
# Consumer: FIFO consumption
key, value, lease = await queue.get()
print(f"Processing {key}: {value}")
try:
# Do work...
await process(value)
# Success: acknowledge
await queue.ack(lease)
except Exception:
# Failure: release for retry
await queue.release(lease, requeue_front=True)
# Targeted consumption
key, value, lease = await queue.take("task-2")
print(f"Specifically got {key}: {value}")
await queue.ack(lease)
asyncio.run(main())
Core Concepts
States
Each key is in exactly one state:
- ABSENT: Not in queue
- AVAILABLE: Ready for consumption
- IN_FLIGHT: Leased to a consumer
API Overview
Producer API
await queue.put(key, value, if_in_flight="update") # update|reject|buffer
Consumer API
# FIFO consumption
key, value, lease = await queue.get(timeout=10.0, lease_timeout=30.0)
# Keyed consumption
key, value, lease = await queue.take("specific-key", timeout=10.0)
Lease Control
await queue.ack(lease) # Permanent removal
await queue.release(lease, requeue_front=True) # Retry
Introspection
value = await queue.peek("key")
has_key = await queue.contains("key")
keys = await queue.available_keys()
inflight = await queue.inflight_keys()
size = await queue.qsize()
In-Flight Policies
Control behavior when putting a key that's currently leased:
update(default): Update the in-flight valuereject: RaiseKeyAlreadyInFlightErrorbuffer: Enqueue a second copy to available
await queue.put("key", new_value, if_in_flight="update")
Lease Timeouts
Prevent stuck items with automatic lease expiration:
# Queue-wide default timeout
queue = LeasedKeyQueue[str, int](default_lease_timeout=30.0)
await queue.start()
# Per-lease override
key, value, lease = await queue.get(lease_timeout=60.0)
Expired leases are automatically released to the front of the queue for retry.
Complexity Guarantees
| Operation | Time Complexity |
|---|---|
put() |
O(1) |
get() |
O(1) |
take(key) |
O(1) |
ack() |
O(1) |
release() |
O(1) |
Achieved through intrusive doubly-linked list with direct node references.
Use Cases
- Task Queues: FIFO processing with retry on failure
- Job Scheduling: Target specific jobs while maintaining order
- Rate Limiting: Lease-based exclusive access prevents double-processing
- Event Processing: Handle events by ID with guaranteed exclusivity
- Workflow Engines: Track in-flight work with timeout-based recovery
Documentation
Development
# Install with dev dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Type checking
mypy src/
# Linting
ruff check src/ tests/
License
MIT License - see LICENSE file for details.
Contributing
Contributions welcome! Please open an issue or PR on GitHub.
Metadata
Release files for leasedkeyq 0.0.7
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 | |
|---|---|---|---|---|
| leasedkeyq-0.0.7.tar.gz | 68.1 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| leasedkeyq-0.0.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 78.4 kB
Release files / leasedkeyq-0.0.7.tar.gz
| Download URL | leasedkeyq-0.0.7.tar.gz |
|---|---|
| Size | 68.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8619c89dcb021556cea0fc1a3ad359b342c0b4890994c50d4b267c82f8d40bcb
|
|
BLAKE2b-256 checksum How to use checksums |
6d2a058c321eb35995f0f6c5e7eb4fde7680a37b911820ba0c724038881f2870
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jan 2, 2026.
Transparency logRelease files / leasedkeyq-0.0.7-py3-none-any.whl
| Download URL | leasedkeyq-0.0.7-py3-none-any.whl |
|---|---|
| Size | 10.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bb7abe70306f088965f064099aad23e30750061b6a4e1a5ee311700890b65e38
|
|
BLAKE2b-256 checksum How to use checksums |
13045d7c3484102acaf2f18a5e94394420aec1f0217d11a07b6ebca518b432cc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jan 2, 2026.
Transparency logSecurity for leasedkeyq 0.0.7
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.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for leasedkeyq-0.0.7.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jan 2, 2026.
Transparency logA 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.