Determine the actual number of CPUs available based on systemd resource limits - more accurate than nproc in shared systems with CPU quotas
Project description
CPU Quotas (soma login nodes)
The soma login nodes are shared resources. The total CPU resources are soft-limited in order to support multiple users. This is done using systemd quotas.
Unfortunately, Python libraries and command line utilities like nproc don't know about the soft limits and provide inaccurate user CPU core values, leading to oversubscribe processes when running custom parallel scripts.
Workaround
In this repository you will find some useful scripts that get the user CPU qouta values for soma login nodes:
1. python (get_available_cpus.py)
This script supports multiple usage modes and options:
Features:
- Reads systemd CPU quota from
/run/systemd/system/user-$UID.slice.d/50-CPUQuota.conf - Handles both percentage format (e.g., "200%") and decimal format (e.g., "2.0")
- Provides a conservative mode that reserves CPU for system overhead
- Can be imported as a module in other
Pythonscripts - Includes a quiet mode ideal for scripting
Usage:
# Make executable
chmod +x get_available_cpus.py
# Basic usage
./get_available_cpus.py
# Conservative mode (reserves some CPU for system)
./get_available_cpus.py --conservative
# Quiet mode (just the number)
./get_available_cpus.py --quiet
In python scripts:
from get_available_cpus import get_parallel_workers
# Use in multiprocessing
import multiprocessing as mp
num_workers = get_parallel_workers()
with mp.Pool(num_workers) as pool:
# your parallel code here
2. bash (get_available_cpus.sh)
This is a bash version of the script above for command-line usage:
Usage:
# Make executable
chmod +x get_available_cpus.sh
# Basic usage
./get_available_cpus.sh
# Use in other scripts
NUM_CPUS=$(./get_available_cpus.sh --quiet)
3. pip installing cpu_quota
How to install:
python -m pip install cpu_quota
Usage:
from cpu_quota import get_parallel_workers
num_workers = get_parallel_workers()
Features:
- Systemd Integration: Reads actual CPU quota limits rather than just hardware CPU count
- Fallback: Falls back to nproc if no systemd limits are found
- Conservative Mode: Optionally reserves CPU capacity for system overhead
- Error Handling: Gracefully handles missing files or parsing errors
- Flexible Output: Quiet mode for scripting, verbose mode for debugging
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 cpu_quota-0.1.0.tar.gz.
File metadata
- Download URL: cpu_quota-0.1.0.tar.gz
- Upload date:
- Size: 7.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
affe53ccc78f65974dbc1f3bc90711b89d5f343e790996e345af88254fe732ef
|
|
| MD5 |
d133fdb13cd3a9b633d8eee98c6267d8
|
|
| BLAKE2b-256 |
f23575899b4012cef115cfbc9e1ec3b977b204ad2d2284ec4c72577c13285c1f
|
Provenance
The following attestation bundles were made for cpu_quota-0.1.0.tar.gz:
Publisher:
publish-package.yml on mpinb/howto-cpu-quota
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cpu_quota-0.1.0.tar.gz -
Subject digest:
affe53ccc78f65974dbc1f3bc90711b89d5f343e790996e345af88254fe732ef - Sigstore transparency entry: 325303133
- Sigstore integration time:
-
Permalink:
mpinb/howto-cpu-quota@59a2252d29ce90dfa163762811028449a0d9428b -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/mpinb
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-package.yml@59a2252d29ce90dfa163762811028449a0d9428b -
Trigger Event:
push
-
Statement type:
File details
Details for the file cpu_quota-0.1.0-py3-none-any.whl.
File metadata
- Download URL: cpu_quota-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bbe5e7ae165fb8b9ad1f8d0923a41b1e285eaec13a0138fbb717cc3774a80734
|
|
| MD5 |
b976d741b982cffcad86619800c304e3
|
|
| BLAKE2b-256 |
ec5423e9d5c801a52d82afeb66a0c232cd01861f190aadb6c70c811f5ca1c8e7
|
Provenance
The following attestation bundles were made for cpu_quota-0.1.0-py3-none-any.whl:
Publisher:
publish-package.yml on mpinb/howto-cpu-quota
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cpu_quota-0.1.0-py3-none-any.whl -
Subject digest:
bbe5e7ae165fb8b9ad1f8d0923a41b1e285eaec13a0138fbb717cc3774a80734 - Sigstore transparency entry: 325303190
- Sigstore integration time:
-
Permalink:
mpinb/howto-cpu-quota@59a2252d29ce90dfa163762811028449a0d9428b -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/mpinb
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-package.yml@59a2252d29ce90dfa163762811028449a0d9428b -
Trigger Event:
push
-
Statement type: