Resource lock manager for coordinating shared system resources (GPU VRAM, RAM, CPU) across processes
Project description
reslock
Resource lock manager for coordinating shared system resources (GPU VRAM, RAM, CPU cores) across multiple processes on a single machine.
Problem
Multiple GPU-consuming processes (llama.cpp, whisper, vLLM, training jobs) compete for limited resources — especially VRAM. Without coordination, they OOM or degrade each other.
How it works
- All coordination happens through a single JSON state file — no daemon required
- Processes coordinate via file locking (held only during reads/writes, not for lease duration)
- Dead processes are automatically cleaned up via PID checking
- Priority queue determines which waiter gets resources next
- Reclaimable leases allow loaded models to be preempted by higher-priority work
Install
pip install reslock
Python API
from reslock import ResourcePool
pool = ResourcePool() # uses ~/.reslock/state.json
# Context manager — blocks until resources are available
with pool.acquire(vram_mb=4000, priority=5, label="whisper") as lease:
run_whisper(audio_file)
# Non-blocking
lease = pool.try_acquire(vram_mb=4000)
if lease:
try:
do_work()
finally:
lease.release()
# Async
async with pool.acquire_async(vram_mb=4000) as lease:
await run_inference()
# Reclaimable lease — can be preempted
lease = pool.acquire(vram_mb=4000, reclaimable=True)
load_model()
# ... later:
if lease.reclaim_requested:
unload_model()
lease.release()
# Check status
status = pool.status()
print(status.available) # free resources
CLI
# Initialize (auto-detects GPU)
reslock init
# Set resources manually
reslock set vram_mb 24000
reslock set gpu_slots 2
# Show status
reslock status
# Run a command with reserved resources
reslock run --vram 4G llama-cli --model model.gguf
reslock run --vram 8G --priority 10 --label "llama-70b" llama-cli ...
reslock run --vram 4G --ram 16G --cpu 4 python train.py
# Manage leases
reslock list
reslock release abc-123
reslock release --label whisper
reslock reset
How resources work
Resources are named quantities with a total capacity. Resource names are arbitrary strings — define whatever you need:
reslock set vram_mb 24000
reslock set ram_mb 65536
reslock set gpu_slots 2
Leases reserve amounts from these pools. When a lease is released (or its process dies), the resources become available again.
Priority queue
When resources aren't immediately available, requests enter a priority queue. Higher priority number = more urgent. Ties are broken by arrival time (FIFO).
Reclaimable leases
A process can mark its lease as reclaimable — "I'm using this, but can give it up if needed." When a higher-priority request needs those resources, reclaim_requested is set to True. The lease holder cooperates by releasing.
Docker
Containers need access to the shared state file. Mount it (and its directory) from the host:
docker run --pid=host \
-v ~/.reslock:/root/.reslock \
my-gpu-app
--pid=host— Required so the host can check container PIDs for dead-process cleanup. Without it, container PIDs are invisible to the host and leases won't be cleaned up when containers exit.-v ~/.reslock:/root/.reslock— Mounts the state file directory. All containers and the host share the samestate.json. The mount path inside the container must match thestate_pathused by reslock (default:~/.reslock/state.json).
Multi-user: The state directory is created with mode 1777 (world-writable + sticky bit, like /tmp) and the state file with mode 666, so multiple containers running as different UIDs can share it without permission issues.
If your container runs as a non-root user, mount to that user's home directory instead:
docker run --pid=host \
-v ~/.reslock:/home/appuser/.reslock \
my-gpu-app
Or use a custom state path shared between host and containers:
# Both host and container code use the same explicit path
pool = ResourcePool(state_path="/shared/reslock/state.json")
docker run --pid=host \
-v /shared/reslock:/shared/reslock \
my-gpu-app
Development
uv venv && uv pip install -e ".[dev]"
pytest
ruff check src/ tests/
Project details
Release history Release notifications | RSS feed
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 reslock-0.2.1.tar.gz.
File metadata
- Download URL: reslock-0.2.1.tar.gz
- Upload date:
- Size: 13.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a42f68eb9db234e2baa2d23682e38f6eaa15a71762d8e789969ba2dbfb2b469a
|
|
| MD5 |
2248d3aaa45563248adf4dd9571cf3b6
|
|
| BLAKE2b-256 |
955392ef880f406829b2f3bae08003c963115a2b5a1dab1d9cc1427de4812c3e
|
Provenance
The following attestation bundles were made for reslock-0.2.1.tar.gz:
Publisher:
publish.yml on mo22/reslock
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
reslock-0.2.1.tar.gz -
Subject digest:
a42f68eb9db234e2baa2d23682e38f6eaa15a71762d8e789969ba2dbfb2b469a - Sigstore transparency entry: 1216342406
- Sigstore integration time:
-
Permalink:
mo22/reslock@9a308a1f6812171f2b7d130a239269bb3d5969ce -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/mo22
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@9a308a1f6812171f2b7d130a239269bb3d5969ce -
Trigger Event:
release
-
Statement type:
File details
Details for the file reslock-0.2.1-py3-none-any.whl.
File metadata
- Download URL: reslock-0.2.1-py3-none-any.whl
- Upload date:
- Size: 16.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dcf34d874f238ffe7208d7563a208885f6e9b8545daf8b84d7b23cd5c1f847a7
|
|
| MD5 |
8d37600eed40a03f807d8e48be64fb94
|
|
| BLAKE2b-256 |
47cca80609cf549ee063a3e77f085c849fd42a677332c783237e50e091516d44
|
Provenance
The following attestation bundles were made for reslock-0.2.1-py3-none-any.whl:
Publisher:
publish.yml on mo22/reslock
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
reslock-0.2.1-py3-none-any.whl -
Subject digest:
dcf34d874f238ffe7208d7563a208885f6e9b8545daf8b84d7b23cd5c1f847a7 - Sigstore transparency entry: 1216342485
- Sigstore integration time:
-
Permalink:
mo22/reslock@9a308a1f6812171f2b7d130a239269bb3d5969ce -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/mo22
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@9a308a1f6812171f2b7d130a239269bb3d5969ce -
Trigger Event:
release
-
Statement type: