diff options
author | Victor Stinner <vstinner@python.org> | 2023-09-30 22:12:51 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-30 22:12:51 (GMT) |
commit | c81521020d643b4a5183098470ef7e6470facefb (patch) | |
tree | 9679b712dd9f4bfa003d6c8a0b188d34cf015025 /Lib/os.py | |
parent | 2c234196ea30b9da370780204ed9068f1fb134c6 (diff) | |
download | cpython-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 'Lib/os.py')
-rw-r--r-- | Lib/os.py | 14 |
1 files changed, 14 insertions, 0 deletions
@@ -1136,3 +1136,17 @@ if name == 'nt': cookie, nt._remove_dll_directory ) + + +if _exists('sched_getaffinity'): + def process_cpu_count(): + """ + Get the number of CPUs of the current process. + + Return the number of logical CPUs usable by the calling thread of the + current process. Return None if indeterminable. + """ + return len(sched_getaffinity(0)) +else: + # Just an alias to cpu_count() (same docstring) + process_cpu_count = cpu_count |