Registry / devops / cpuset-py3

cpuset-py3

JSON →
library1.0.2pypypi✓ verified 82d ago

cpuset-py3 is a Python 3 compatible fork of the original cpuset library, providing functionalities to query and manipulate CPU sets (processor affinity) on Linux systems. It allows creating, modifying, and applying CPU sets to processes and threads, facilitating fine-grained control over CPU core usage. The current version is 1.0.2, and it's maintained on an as-needed basis rather than a strict release cadence.

pip install cpuset-py3
INSTALL
IMPORT
SIG · CPUSET-PY3
C
cpuset-py3
devopspythonv1.0.2
Install
1.6s avg
Import
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 18MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.6s · import 0.000s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

CPUSet
import cpuset
from cpuset import CPUSet

This quickstart demonstrates how to create and manipulate `CPUSet` objects. It also shows how to query the current process's affinity on Linux, with a note about the permissions often required for setting affinity.

import cpuset import platform print(f"Running on OS: {platform.system()}\n") # Create a new empty cpuset s = cpuset.CPUSet() s.add_cpu(0) s.add_cpu(1) s.add_cpu(2) print(f"Created CPUSet (0,1,2): {s.as_list()}") print(f"As string: {s.as_str()}") print(f"As canonical string: {s.as_canonical_str()}") # will be "0-2" print(f"As bitmask: {hex(s.as_bitmask())}") # 0x7 # Create from a string representation s_from_str = cpuset.CPUSet("0-3,5") print(f"\nCPUSet from string '0-3,5': {s_from_str.as_list()}") if platform.system() == "Linux": print("\nAttempting to query current process affinity (Linux-only):") try: current_affinity = cpuset.get_cpuset_for_current_process() print(f" Current process affinity: {current_affinity.as_list()}") # Uncomment to attempt setting affinity (requires appropriate permissions): # print(f" Attempting to set process affinity to {s.as_list()}...") # cpuset.set_cpuset_for_current_process(s) # updated_affinity = cpuset.get_cpuset_for_current_process() # print(f" Updated process affinity: {updated_affinity.as_list()}") except PermissionError: print(" Permission denied to get/set process affinity. Try running with sudo or check capabilities.") except Exception as e: print(f" An error occurred during system affinity call: {e}") else: print("\nSystem affinity functions (get/set) are only available on Linux.") print("\nOperations on CPUSet objects are generally platform-independent:") s_union = s | cpuset.CPUSet("3-4") print(f"Union (0-2 | 3-4): {s_union.as_list()}") # [0,1,2,3,4] s_intersect = s & cpuset.CPUSet("1-3") print(f"Intersection (0-2 & 1-3): {s_intersect.as_list()}") # [1,2]
cpuset --version
Debug
Known issues
gotchaThis library is designed specifically for Linux systems. Functions for querying and setting CPU affinity (`get_cpuset_for_current_process`, `set_cpuset_for_current_process`, etc.) will not function correctly or might raise errors on other operating systems like macOS or Windows.
fix
Ensure your application runs on a Linux environment when utilizing system affinity features.
affects: All
gotchaSetting CPU affinity for processes or threads often requires elevated privileges (e.g., root access or specific Linux capabilities like CAP_SYS_NICE). Operations like `set_cpuset_for_current_process` might fail with `PermissionError` if not run with sufficient permissions.
fix
Run your script with `sudo` (e.g., `sudo python your_script.py`) or configure the executing user/process with the necessary Linux capabilities.
affects: All
gotchaBe careful to install `cpuset-py3` for Python 3 compatibility. There is an older, incompatible package named `cpuset` on PyPI which is intended for Python 2 and will cause `AttributeError` or unexpected behavior in Python 3 environments.
fix
Always use `pip install cpuset-py3`. If you accidentally installed `cpuset`, uninstall it first with `pip uninstall cpuset`.
affects: All
gotchaThe `as_bitmask()` method returns an integer bitmask where each bit corresponds to a CPU ID. For larger or sparsely populated CPU sets, interpreting this raw bitmask can be complex or unintuitive compared to list or string representations.
fix
For human-readable output, prefer `as_list()` or `as_canonical_str()`. Only use `as_bitmask()` when direct bitwise operations or kernel-level interfaces require it.
affects: All
Upgrade
Version history
1.0.2latest on PyPI · released Dec 1, 2020
Audit
Dependencies

No dependency data recorded yet.

Agent activity
20 hits · last 30 days
node
20
Resources
cpuset-py3 — pip install cpuset-py3 · libregistry