From b370b73cff134830bae0f368c5bd9c6bc0385bf7 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sun, 1 Oct 2023 03:01:53 +0200 Subject: [PATCH 1/2] gh-109649: Enhance os.cpu_count() documentation * Doc: Specify that os.cpu_count() counts *logicial* CPUs. * Doc: Specify that os.sched_getaffinity(0) is related to the calling thread. * Fix test_posix.test_sched_getaffinity(): restore the old CPU mask when the test completes! --- Doc/library/os.rst | 14 ++++++-------- Lib/test/test_posix.py | 1 + 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/Doc/library/os.rst b/Doc/library/os.rst index 4ffd520f9ecd8b..b4114d14e9bb69 100644 --- a/Doc/library/os.rst +++ b/Doc/library/os.rst @@ -5141,8 +5141,10 @@ operating system. .. function:: sched_getaffinity(pid, /) - Return the set of CPUs the process with PID *pid* (or the current process - if zero) is restricted to. + Return the set of CPUs the process with PID *pid* is restricted to. + + If *pid* is zero, return the set of CPUs the calling thread of the current + process is restricted to. .. _os-path: @@ -5183,12 +5185,8 @@ Miscellaneous System Information .. function:: cpu_count() - Return the number of CPUs in the system. Returns ``None`` if undetermined. - - 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))`` - + Return the number of logical CPUs in the system. Returns ``None`` if + undetermined. .. versionadded:: 3.4 diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py index 444f8abe4607b7..9d72dba159c6be 100644 --- a/Lib/test/test_posix.py +++ b/Lib/test/test_posix.py @@ -1205,6 +1205,7 @@ def test_sched_getaffinity(self): @requires_sched_affinity def test_sched_setaffinity(self): mask = posix.sched_getaffinity(0) + self.addCleanup(posix.sched_setaffinity, 0, list(mask)) if len(mask) > 1: # Empty masks are forbidden mask.pop() From f65bc28f5b6d5092230ac74e3730a24cac9c5f15 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sun, 1 Oct 2023 12:49:17 +0200 Subject: [PATCH 2/2] Restore removed text --- Doc/library/os.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Doc/library/os.rst b/Doc/library/os.rst index b4114d14e9bb69..0c36c244ab53db 100644 --- a/Doc/library/os.rst +++ b/Doc/library/os.rst @@ -5188,6 +5188,10 @@ Miscellaneous System Information Return the number of logical CPUs in the system. Returns ``None`` if undetermined. + This number is not equivalent to the number of logical CPUs the current + process can use. ``len(os.sched_getaffinity(0))`` gets the number of logical + CPUs the calling thread of the current process is restricted to + .. versionadded:: 3.4