Resource Semaphore
Resource Semaphore is a typed, high-level synchronization library for managing multiple constrained resources within a single process. It applies backpressure to prevent resource exhaustion.
Standard semaphores guard a single counter of "slots." resource-semaphore extends this to multiple, heterogeneous resources: such as CPU cores, available RAM, disk I/O bandwidth, or worker slots: with a single, atomic acquire/release operation. When capacity is exhausted, callers block until resources become available.
Installation
pip install resource-semaphore
Quick Start
import asyncio
from resource_semaphore import AsyncResourceSemaphore
semaphore = AsyncResourceSemaphore(resources={"db_conn": 2, "ram_mb": 4096})
async def process(data_size_mb: float):
async with semaphore.claim({"db_conn": 1, "ram_mb": data_size_mb}):
await do_work()
async def main():
# Three tasks compete for 2 DB connections: one will wait automatically
await asyncio.gather(
process(1024),
process(1024),
process(1024),
)
Features
- Multi-resource: manage DB connection, RAM, workers, or any named resource in a single semaphore
- Sync + Async:
ResourceSemaphorefor threads,AsyncResourceSemaphorefor asyncio - Fairness & Lookahead: Avoids starvation by enforcing strict head-of-line blocking, with a configurable
lookahead_window(default: 1) that allows smaller queued requests to safely bypass heavy blocked tasks, drastically improving throughput. - Greedy Variants:
GreedyResourceSemaphoreandAsyncGreedyResourceSemaphoreskip queueing entirely for maximum utilization in non-starving pipelines. - High Performance: Built on a shared template base class architecture, heavily optimizing acquisition loops by stripping unnecessary overhead from greedy implementations.
- Timeouts: abort waits gracefully using
timeoutarguments - Safe Releases: opaque
Ticketobjects prevent corrupted state from incorrect releases - Typed: generic over resource key types via
Literalfor compile-time safety - No-op variants:
NoopResourceSemaphoreandAsyncNoopResourceSemaphorefor tests - Graceful shutdown:
shutdown()wakes all blocked callers with aSemaphoreError - Zero dependencies (Core): pure Python, nothing to install beyond the standard library
- System Utilities: dynamic initialization via
resource-semaphore[utils]for fetching CPU/RAM limits
Documentation
For a comprehensive guide on core concepts, initialization, and API usage (including both synchronous and asynchronous context managers), please refer to our documentation:
Examples
We provide runnable scripts demonstrating real-world usage scenarios:
Roadmap
To see what features are planned for future releases and what has already been accomplished, check out our TODO.md tracker.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file resource_semaphore-1.1.0.tar.gz.
File metadata
- Download URL: resource_semaphore-1.1.0.tar.gz
- Upload date:
- Size: 11.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bdcb8dd80d850d9946f153afcc383d4467bbe3df3b8e164a99ce81568e33a02c
|
|
| MD5 |
f5b428f9df665413065e4eb07d32ccdb
|
|
| BLAKE2b-256 |
81fd868afeb0a777d1943af18b175b161fb47ded848127b991ec8182ee6df7cd
|
File details
Details for the file resource_semaphore-1.1.0-py3-none-any.whl.
File metadata
- Download URL: resource_semaphore-1.1.0-py3-none-any.whl
- Upload date:
- Size: 13.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
af34a1cfc1be265937d3d56f5d73d55e36d271fde4e49dc66025cd9237c951d0
|
|
| MD5 |
2ae7cc831e2be9ee1311d6bd36b705a6
|
|
| BLAKE2b-256 |
0524c951d846537d5a13b82eae1d2ca7c9aa4e42bcda28ad9e5f17d826dc4973
|