Skip to content

Commit

Permalink
[3.11] [3.12] gh-109649: Enhance os.cpu_count() documentation (GH-110169
Browse files Browse the repository at this point in the history
) (#110226)

[3.12] gh-109649: Enhance os.cpu_count() documentation (GH-110169)

* 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!

* Restore removed text
(cherry picked from commit 5245b97)

Co-authored-by: Victor Stinner <vstinner@python.org>
  • Loading branch information
miss-islington and vstinner committed Oct 3, 2023
1 parent 981696e commit bf6843e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
16 changes: 9 additions & 7 deletions Doc/library/os.rst
Expand Up @@ -4872,8 +4872,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:
Expand Down Expand Up @@ -4914,12 +4916,12 @@ 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.

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

Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_posix.py
Expand Up @@ -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()
Expand Down

0 comments on commit bf6843e

Please sign in to comment.