Repository navigation
os.process_cpu_count(): get the number CPUs usable by the current process #109649
Copy link
Copy link
Closed
Labels
type-featureA feature request or enhancementA feature request or enhancement
Description
Activity
- addedtype-featureA feature request or enhancementA feature request or enhancement
on Sep 21, 2023 os.cpu_count() issues (open and closed):
- issue add os.cpu_count() #62114: add os.cpu_count()
- issue cpu_count() unreliable on Windows #67226: cpu_count() unreliable on Windows
- issue os.cpu_count is problematic on sparc/solaris #73451: os.cpu_count is problematic on sparc/solaris
- issue os.cpu_count() returns wrong number of processors on system with > 64 logical processors #74766: Windows: os.cpu_count() returns wrong number of processors on system with > 64 logical processors
- issue os.cpu_count() should return a count assigned to a container or that the process is restricted to #80235: On Linux, os.count() should read cgroup cpu.shares and cpu.cfs (CPU count inside docker container)
- issue Support python -Xcpu_count=<n> feature for container environment. #109595: Support python -Xcpu_count= feature for container environment
Previously, the function was known as multiprocessing.cpu_count():
- issue multiprocessing.cpu_count() should use hw.availcpu on Mac OS X #61646: multiprocessing.cpu_count() should use hw.availcpu on Mac OS X
On Windows, maybe it can be implemented with GetProcessGroupAffinity(), GetProcessAffinityMask(), GetActiveProcessorGroupCount() and GetActiveProcessorCount().
GetProcessGroupAffinity():
https://learn.microsoft.com/en-us/windows/win32/api/processtopologyapi/nf-processtopologyapi-getprocessgroupaffinityOn Windows, a CPU affinity can be run with
start /affinitycommand. Example to only run Python on the firstCPU:start /affinity 0x0000000000000001 python.exe- added a commit that references this issue
on Sep 21, 2023 - changed the title
[-]os.cpu_count(): add "usable" parameter to get the number of "usable" CPUs[/-][+]os.cpu_count(): add "affinity" parameter to get the number of logical CPUs usable by the current process[/+]on Sep 21, 2023 - added 6 commits that reference this issue
on Sep 21, 2023 5 remaining items
- added 6 commits that reference this issue
on Sep 30, 2023
Metadata
Metadata
Assignees
Labels
type-featureA feature request or enhancementA feature request or enhancement
Feature or enhancement
os.cpu_count() returns the total number of CPUs that the current machine has. On Unix, sched_setaffinity() can reduce the number of CPU "usable" by a process. Currently, os.cpu_count() docstring contains a recipe how to get the number of usable CPUs:
I propose to add an affinity optional keyword-only parameter to os.cpu_count() which would return
len(os.sched_getaffinity(0)).stUPDATE: Rename usable parameter to affinity.
Linked PRs