You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
os.process_cpu_count(): get the number CPUs usable by the current process #109649
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:
>>> import os; help(os.cpu_count)
cpu_count()
Return the number of CPUs in the system; return None if indeterminable.
This number is not equivalent to the number of CPUs the current process can
use. The number of usable CPUs can be obtained with
``len(os.sched_getaffinity(0))``
I propose to add an affinity optional keyword-only parameter to os.cpu_count() which would return len(os.sched_getaffinity(0)).st
On Windows, maybe it can be implemented with GetProcessGroupAffinity(), GetProcessAffinityMask(), GetActiveProcessorGroupCount() and GetActiveProcessorCount().
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
Activity
vstinner commentedon Sep 21, 2023
os.cpu_count() issues (open and closed):
Previously, the function was known as multiprocessing.cpu_count():
pythongh-109649: Add usable parameter to os.cpu_count()
pythongh-109649: Add usable parameter to os.cpu_count()
pythongh-109649: Add usable parameter to os.cpu_count()
vstinner commentedon Sep 21, 2023
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-getprocessgroupaffinity
vstinner commentedon Sep 21, 2023
On Windows, a CPU affinity can be run with
start /affinitycommand. Example to only run Python on the firstCPU:pythongh-109649: Add affinity parameter to os.cpu_count()
[-]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[/+]pythongh-109649: Add affinity parameter to os.cpu_count()
pythongh-109649: Add affinity parameter to os.cpu_count()
pythongh-109649: Add affinity parameter to os.cpu_count()
pythongh-109649: Add os.process_cpu_count() function
pythongh-109649: Add os.process_cpu_count() function
pythongh-109649: Add os.process_cpu_count() function
5 remaining items
pythongh-109649: Use os.process_cpu_count()
pythongh-109649: Use os.process_cpu_count()
pythongh-109649: Use os.process_cpu_count()
pythongh-109649: Enhance os.cpu_count() documentation
pythongh-109649: Enhance os.cpu_count() documentation
gh-109649: Use os.process_cpu_count() (#110165)
[3.12] gh-109649: Enhance os.cpu_count() documentation (#110169)
[3.12] pythongh-109649: Enhance os.cpu_count() documentation (pythonG…
[3.11] [3.12] gh-109649: Enhance os.cpu_count() documentation (GH-110169
pythongh-109649: Fix test_os.test_process_cpu_count_affinity()
gh-109649: Fix test_os.test_process_cpu_count_affinity() (#111689)
pythongh-109649: Fix test_os.test_process_cpu_count_affinity() (pytho…
pythongh-109649: Add os.process_cpu_count() function (python#109907)
pythongh-109649: Use os.process_cpu_count() (python#110165)
pythongh-109649: Fix test_os.test_process_cpu_count_affinity() (pytho…