From f73a6cfcd97a84543bb87ac5bbbbc77ee09b61c0 Mon Sep 17 00:00:00 2001 From: Julian Taylor Date: Sat, 7 Mar 2015 12:00:03 +0100 Subject: [PATCH] Issue #23530: Update documentation clarify relation of cpu_count and sched_getaffinity cpu_count is often misused to determine the number of usable cpus which leads to oversubscription on systems that limit processes to only subset of online cpus. --- Doc/library/multiprocessing.rst | 8 ++++++-- Doc/library/os.rst | 4 ++++ Modules/posixmodule.c | 7 +++++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Doc/library/multiprocessing.rst b/Doc/library/multiprocessing.rst index 4b829af..af6315e 100644 --- a/Doc/library/multiprocessing.rst +++ b/Doc/library/multiprocessing.rst @@ -869,8 +869,12 @@ Miscellaneous .. function:: cpu_count() - Return the number of CPUs in the system. May raise - :exc:`NotImplementedError`. + Return the number of CPUs in the system. + + This number is not equivalent to the number of CPUs a process can use. + The number of usable CPUs can be obtained with ``len(os.sched_getaffinity(0))`` + + May raise :exc:`NotImplementedError`. .. seealso:: :func:`os.cpu_count` diff --git a/Doc/library/os.rst b/Doc/library/os.rst index 2467c60..546aeca 100644 --- a/Doc/library/os.rst +++ b/Doc/library/os.rst @@ -3449,6 +3449,10 @@ Miscellaneous System Information Return the number of CPUs in the system. Returns None if undetermined. + This number is not equivalent to the number of CPUs a process can use. + The number of usable CPUs can be obtained with ``len(os.sched_getaffinity(0))`` + + .. versionadded:: 3.4 diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 679fc8f..685b615 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -7687,7 +7687,7 @@ os.sched_getaffinity pid: pid_t / -Return the affinity of the process identified by pid. +Return the affinity of the process identified by pid (or the current process if zero). The affinity is returned as a set of CPU identifiers. [clinic start generated code]*/ @@ -7696,7 +7696,8 @@ PyDoc_STRVAR(os_sched_getaffinity__doc__, "sched_getaffinity($module, pid, /)\n" "--\n" "\n" -"Return the affinity of the process identified by pid.\n" +"Return the affinity of the process identified by pid " +"(or the current process if zero).\n" "\n" "The affinity is returned as a set of CPU identifiers."); @@ -16016,6 +16017,8 @@ get_terminal_size(PyObject *self, PyObject *args) os.cpu_count Return the number of CPUs in the system; return None if indeterminable. +This number is not equivalent to the number of CPUs a process can use. +The number of usable CPUs can be obtained with ``len(os.sched_getaffinity(0))`` [clinic start generated code]*/ PyDoc_STRVAR(os_cpu_count__doc__, -- 2.1.0