-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
os.process_cpu_count(): get the number CPUs usable by the current process #109649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
os.cpu_count() issues (open and closed):
Previously, the function was known as multiprocessing.cpu_count():
|
Fix test_posix.test_sched_getaffinity(): restore the old CPU mask when the test completes!
On Windows, maybe it can be implemented with GetProcessGroupAffinity(), GetProcessAffinityMask(), GetActiveProcessorGroupCount() and GetActiveProcessorCount(). GetProcessGroupAffinity(): |
On Windows, a CPU affinity can be run with
|
Implement cpu_count(affinity=True) with sched_getaffinity() on Unix and GetProcessAffinityMask() on Windows. Changes: * Fix test_posix.test_sched_getaffinity(): restore the old CPU mask when the test completes! * Doc: Specify that os.cpu_count() counts *logicial* CPUs and mention that Linux cgroups are ignored. * _Py_popcount32() uses UINT32_C() for M1, M2 and M4 constants. * Add _Py_popcount64(). Add tests on _Py_popcount64().
Implement cpu_count(affinity=True) with sched_getaffinity() on Unix and GetProcessAffinityMask() on Windows. Changes: * Fix test_posix.test_sched_getaffinity(): restore the old CPU mask when the test completes! * Doc: Specify that os.cpu_count() counts *logicial* CPUs and mention that Linux cgroups are ignored. * _Py_popcount32() uses UINT32_C() for M1, M2 and M4 constants. * Add _Py_popcount64(). Add tests on _Py_popcount64().
Implement cpu_count(affinity=True) with sched_getaffinity() on Unix and GetProcessAffinityMask() on Windows. Changes: * Fix test_posix.test_sched_getaffinity(): restore the old CPU mask when the test completes! * Doc: Specify that os.cpu_count() counts *logicial* CPUs and mention that Linux cgroups are ignored. * _Py_popcount32() uses UINT32_C() for M1, M2 and M4 constants. * Add _Py_popcount64(). Add tests on _Py_popcount64().
Implement cpu_count(affinity=True) with sched_getaffinity(). Changes: * Fix test_posix.test_sched_getaffinity(): restore the old CPU mask when the test completes! * Doc: Specify that os.cpu_count() counts *logicial* CPUs. Mention that Linux cgroups are ignored.
* Fix test_posix.test_sched_getaffinity(): restore the old CPU mask when the test completes! * Doc: Specify that os.cpu_count() counts *logicial* CPUs.
* Fix test_posix.test_sched_getaffinity(): restore the old CPU mask when the test completes! * Doc: Specify that os.cpu_count() counts *logicial* CPUs.
* Fix test_posix.test_sched_getaffinity(): restore the old CPU mask when the test completes! * Doc: Specify that os.cpu_count() counts *logicial* CPUs.
* Fix test_posix.test_sched_getaffinity(): restore the old CPU mask when the test completes! * Doc: Specify that os.cpu_count() counts *logicial* CPUs.
* 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.
* 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.
Replace os.cpu_count() with os.process_cpu_count() in modules: * compileall * concurrent.futures * multiprocessing Replace os.cpu_count() with os.process_cpu_count() in programs: * _decimal deccheck.py test * freeze.py * multissltests.py * python -m test (regrtest) * wasm_build.py Other changes: * test.pythoninfo logs os.process_cpu_count() * regrtest gets os.process_cpu_count() / os.cpu_count() in headers.
Replace os.cpu_count() with os.process_cpu_count() in modules: * compileall * concurrent.futures * multiprocessing Replace os.cpu_count() with os.process_cpu_count() in programs: * _decimal deccheck.py test * freeze.py * multissltests.py * python -m test (regrtest) * wasm_build.py Other changes: * test.pythoninfo logs os.process_cpu_count() * regrtest gets os.process_cpu_count() / os.cpu_count() in headers.
Replace os.cpu_count() with os.process_cpu_count() in modules: * compileall * concurrent.futures * multiprocessing Replace os.cpu_count() with os.process_cpu_count() in programs: * _decimal deccheck.py test * freeze.py * multissltests.py * python -m test (regrtest) * wasm_build.py Other changes: * test.pythoninfo logs os.process_cpu_count(). * regrtest gets os.process_cpu_count() / os.cpu_count() in headers.
* 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: 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!
Replace os.cpu_count() with os.process_cpu_count() in modules: * compileall * concurrent.futures * multiprocessing Replace os.cpu_count() with os.process_cpu_count() in programs: * _decimal deccheck.py test * freeze.py * multissltests.py * python -m test (regrtest) * wasm_build.py Other changes: * test.pythoninfo logs os.process_cpu_count(). * regrtest gets os.process_cpu_count() / os.cpu_count() in headers.
* 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
…H-110169) * pythongh-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>
) (#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>
When CPUs are isolated on Linux, os.process_cpu_count() is smaller than os.cpu_count(). Fix the test for this case. Example with "isolcpus=5,11 rcu_nocbs=5,11" options passed to a Linux command line to isolated two logical CPUs: $ ./python -c 'import os; print(os.process_cpu_count(), "/", os.cpu_count())' 10 / 12
When CPUs are isolated on Linux, os.process_cpu_count() is smaller than os.cpu_count(). Fix the test for this case. Example with "isolcpus=5,11 rcu_nocbs=5,11" options passed to a Linux command line to isolated two logical CPUs: $ ./python -c 'import os; print(os.process_cpu_count(), "/", os.cpu_count())' 10 / 12
…n#111689) When CPUs are isolated on Linux, os.process_cpu_count() is smaller than os.cpu_count(). Fix the test for this case. Example with "isolcpus=5,11 rcu_nocbs=5,11" options passed to a Linux command line to isolated two logical CPUs: $ ./python -c 'import os; print(os.process_cpu_count(), "/", os.cpu_count())' 10 / 12
* 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.
Replace os.cpu_count() with os.process_cpu_count() in modules: * compileall * concurrent.futures * multiprocessing Replace os.cpu_count() with os.process_cpu_count() in programs: * _decimal deccheck.py test * freeze.py * multissltests.py * python -m test (regrtest) * wasm_build.py Other changes: * test.pythoninfo logs os.process_cpu_count(). * regrtest gets os.process_cpu_count() / os.cpu_count() in headers.
…n#111689) When CPUs are isolated on Linux, os.process_cpu_count() is smaller than os.cpu_count(). Fix the test for this case. Example with "isolcpus=5,11 rcu_nocbs=5,11" options passed to a Linux command line to isolated two logical CPUs: $ ./python -c 'import os; print(os.process_cpu_count(), "/", os.cpu_count())' 10 / 12
Feature or enhancement
os.cpu_count() returns the total number of CPUs that the current machine has. On Unix, sched_setaffinity() can reduce the number of CPU "usable" by a process. Currently, os.cpu_count() docstring contains a recipe how to get the number of usable CPUs:
I propose to add an affinity optional keyword-only parameter to os.cpu_count() which would return
len(os.sched_getaffinity(0))
.stUPDATE: Rename usable parameter to affinity.
Linked PRs
The text was updated successfully, but these errors were encountered: