Skip to main content

Resource Semaphore

License Python Coverage

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: int):
    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: atomically acquire multiple named resources (e.g. DB connections, RAM, workers) in one call
  • Sync + Async: ResourceSemaphore (threading) and AsyncResourceSemaphore (asyncio) share the same API
  • Fair and Greedy variants: Fair semaphores wake the earliest-arrived request that can currently be satisfied, and let smaller later requests bypass a blocked larger one — see Docs: Fairness and Trade-offs for the starvation trade-off this implies. Greedy semaphores skip ordering entirely and grant to whichever caller wins the race; this can starve a specific waiter indefinitely under sustained contention (see Docs: Fairness and Trade-offs).
  • Timeouts: acquire()/claim() accept an optional timeout argument and raise TimeoutError
  • Safe releases: opaque Ticket objects; releasing an unknown or already-released ticket raises ValueError
  • Typed: generic over resource key types via Literal for compile-time safety
  • No-op variants: NoopResourceSemaphore and AsyncNoopResourceSemaphore for tests that don't need real limits
  • Graceful shutdown: shutdown() wakes all blocked callers with SemaphoreError; further acquire() calls raise immediately
  • Zero dependencies in core
  • Optional system utilities: resource-semaphore[utils] wraps psutil for CPU/RAM/disk capacity discovery (CPU and RAM only — no GPU support)
  • Performance: Fair and Greedy variants show no measurable throughput difference in our benchmarks (see .benchmarks/); Greedy trades ordering guarantees for a simpler code path, not raw speed.

Limitations

  • Single-process only: Manages concurrency within a single Python process (threads or asyncio tasks). It is not a distributed or cross-process semaphore.
  • Resource discovery limits: Built-in utilities (resource-semaphore[utils]) only discover basic CPU, memory, and disk metrics via psutil. There is no automatic discovery for GPUs, network interfaces, or custom external hardware (though any named resource can be managed by manually specifying integer capacities).
  • No deadlock prevention: Does not detect or prevent application-level deadlocks resulting from improper lock acquisition ordering across multiple semaphores or code paths.
  • Evolving API: The API is subject to refinement; minor version updates may introduce breaking changes as feature needs evolve.

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

resource_semaphore-1.3.1.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

resource_semaphore-1.3.1-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

File details

Details for the file resource_semaphore-1.3.1.tar.gz.

File metadata

  • Download URL: resource_semaphore-1.3.1.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","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

Hashes for resource_semaphore-1.3.1.tar.gz
Algorithm Hash digest
SHA256 9db775b051b6c336dbe0e572a79f22d6da968a222553f285b0dccaecebef1025
MD5 e429207a558ee91f62027aa60df9df7c
BLAKE2b-256 aa510e8e4fc264e718cb8e352fd3bce76f2081f887f5afa20ccc0f4244e8f880

See more details on using hashes here.

File details

Details for the file resource_semaphore-1.3.1-py3-none-any.whl.

File metadata

  • Download URL: resource_semaphore-1.3.1-py3-none-any.whl
  • Upload date:
  • Size: 15.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","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

Hashes for resource_semaphore-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9ebe23848a2cf5072a14b56be713e585e812d4684d757e6c731a9766d495f61f
MD5 e716eac8e750373ec50c73e3c2e81d94
BLAKE2b-256 df8518fac6b087ae92a89efbc1f4b0389d6a0037752637f70f736f51ea14c4e9

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.3.1 This release

2 files

1.3.0

2 files

1.1.0

2 files

1.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page