Skip to main content

Memory-budget-aware parallel execution pool for Python

Project description

budgetpool

CI PyPI Python License

Memory-budget-aware parallel execution pool for Python.

A drop-in replacement for ProcessPoolExecutor that calculates safe worker counts based on available memory, prevents OOM crashes, and provides backpressure on task submission.

Why?

ProcessPoolExecutor spawns workers based on CPU count, ignoring memory. When each worker loads a large dataset or model, this easily causes OOM kills:

# Dangerous — 8 workers × 3GB each = 24GB on a 16GB machine
with ProcessPoolExecutor(max_workers=os.cpu_count()) as pool:
    results = list(pool.map(run_backtest, param_grid))

BudgetPool fixes this by computing safe worker counts from your memory budget:

from budgetpool import BudgetPool

# Safe — fits within 12GB, 2GB per worker → 6 workers max
with BudgetPool(memory_budget_gb=12.0, memory_per_worker_gb=2.0) as pool:
    results = list(pool.map(run_backtest, param_grid))

Install

pip install budgetpool

Quick Start

Basic Usage

from budgetpool import BudgetPool

with BudgetPool(memory_per_worker_gb=2.0) as pool:
    results = list(pool.map(heavy_func, items))
    print(f"Used {pool.num_workers} workers")

With Explicit Budget

with BudgetPool(
    memory_budget_gb=12.0,      # Total budget for all workers
    memory_per_worker_gb=2.0,   # Estimated peak per worker
    max_workers=8,              # CPU cap (optional)
) as pool:
    futures = [pool.submit(process, item) for item in items]
    results = [f.result() for f in futures]

Memory Monitoring

from budgetpool import get_memory_info, safe_worker_count

# Check system memory
info = get_memory_info()
print(f"Total: {info.total_gb:.1f}GB, Available: {info.available_gb:.1f}GB")
print(f"Safe for workers: {info.free_for_workers_gb:.1f}GB")

# Calculate worker count without creating a pool
n = safe_worker_count(memory_per_worker_gb=3.0)
print(f"Safe worker count: {n}")

How It Works

  1. Startup: Reads system memory (via psutil), respects cgroup limits in containers
  2. Worker calculation: min(budget ÷ per_worker, cpu_count, max_workers)
  3. Backpressure: Blocks submit() when pending tasks exceed max_pending (default: 2× workers)
  4. Runtime checks: Warns at 85% memory usage, raises MemoryBudgetExceeded at 95%

API Reference

BudgetPool

Parameter Type Default Description
memory_budget_gb float | None Auto-detect Total memory budget for all workers
memory_per_worker_gb float 1.0 Estimated peak memory per worker
max_workers int | None CPU count Hard cap on worker count
max_pending int | None 2 × workers Backpressure threshold
warn_at_percent float | None 85.0 Log warning at this memory %
fail_at_percent float | None 95.0 Raise error at this memory %
mp_context None Multiprocessing start method

Methods: submit(), map(), shutdown() — same signatures as ProcessPoolExecutor.

Properties: num_workers, memory_budget_gb, memory_info.

safe_worker_count(memory_per_worker_gb, max_workers=None) → int

Standalone function to calculate safe worker count without creating a pool.

get_memory_info() → MemoryInfo

Returns a MemoryInfo dataclass with total_gb, available_gb, used_gb, percent, and free_for_workers_gb.

Container Support

budgetpool automatically detects cgroup v1/v2 memory limits, so it works correctly inside Docker containers where psutil.virtual_memory().total would report host memory:

docker run --memory=4g python -c "
from budgetpool import get_memory_info
print(get_memory_info().total_gb)  # → 4.0, not host memory
"

Requirements

  • Python 3.10+
  • psutil ≥ 5.9.0

License

Apache-2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

budgetpool-0.1.0.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

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

budgetpool-0.1.0-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file budgetpool-0.1.0.tar.gz.

File metadata

  • Download URL: budgetpool-0.1.0.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for budgetpool-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f2a2f872cfab025bb3232ac4c4b83843c7f0d737553553707249c3c9577f98ff
MD5 18be52016bdba0f0375960091cf95002
BLAKE2b-256 02f28356e319dde0cef6fd5738f6045d10f12d0eaadddcdf51c7e748b56a3fb8

See more details on using hashes here.

File details

Details for the file budgetpool-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: budgetpool-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for budgetpool-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c2d3951e8fd1e1281187d150bf757c60111148560a8dcf271f55d9e512e4244c
MD5 7f795b33561f2ee599a7f2c5d0b69afa
BLAKE2b-256 ef596e48d1e5a6976e5a63773e678bf066a76956c88b440c1bbbfa65d3634bdf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page