summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2023-09-30 22:12:51 (GMT)
committerGitHub <noreply@github.com>2023-09-30 22:12:51 (GMT)
commitc81521020d643b4a5183098470ef7e6470facefb (patch)
tree9679b712dd9f4bfa003d6c8a0b188d34cf015025 /Doc
parent2c234196ea30b9da370780204ed9068f1fb134c6 (diff)
downloadcpython-c81521020d643b4a5183098470ef7e6470facefb.zip
cpython-c81521020d643b4a5183098470ef7e6470facefb.tar.gz
cpython-c81521020d643b4a5183098470ef7e6470facefb.tar.bz2
gh-109649: Add os.process_cpu_count() function (#109907)
* Refactor os_sched_getaffinity_impl(): move variable definitions to their first assignment. * Fix test_posix.test_sched_getaffinity(): restore the old CPU mask when the test completes! * Doc: Specify that os.cpu_count() counts *logicial* CPUs. * Doc: Specify that os.sched_getaffinity(0) is related to the calling thread.
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/os.rst31
-rw-r--r--Doc/whatsnew/3.13.rst7
2 files changed, 31 insertions, 7 deletions
diff --git a/Doc/library/os.rst b/Doc/library/os.rst
index 4ffd520..141ab0b 100644
--- a/Doc/library/os.rst
+++ b/Doc/library/os.rst
@@ -5141,8 +5141,12 @@ 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.
+
+ See also the :func:`process_cpu_count` function.
.. _os-path:
@@ -5183,12 +5187,11 @@ 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.
+ The :func:`process_cpu_count` function can be used to get the number of
+ logical CPUs usable by the calling thread of the **current process**.
.. versionadded:: 3.4
@@ -5202,6 +5205,20 @@ Miscellaneous System Information
.. availability:: Unix.
+.. function:: process_cpu_count()
+
+ Get the number of logical CPUs usable by the calling thread of the **current
+ process**. Returns ``None`` if undetermined. It can be less than
+ :func:`cpu_count` depending on the CPU affinity.
+
+ The :func:`cpu_count` function can be used to get the number of logical CPUs
+ in the **system**.
+
+ See also the :func:`sched_getaffinity` functions.
+
+ .. versionadded:: 3.13
+
+
.. function:: sysconf(name, /)
Return integer-valued system configuration values. If the configuration value
diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst
index 56618b9..484443a 100644
--- a/Doc/whatsnew/3.13.rst
+++ b/Doc/whatsnew/3.13.rst
@@ -163,6 +163,13 @@ opcode
documented or exposed through ``dis``, and were not intended to be
used externally.
+os
+--
+
+* Add :func:`os.process_cpu_count` function to get the number of logical CPUs
+ usable by the calling thread of the current process.
+ (Contributed by Victor Stinner in :gh:`109649`.)
+
pathlib
-------