-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-109649: Add affinity parameter to os.cpu_count() #109652
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5153,14 +5153,21 @@ Miscellaneous System Information | |
.. availability:: Unix. | ||
|
||
|
||
.. function:: cpu_count() | ||
.. function:: cpu_count(*, affinity=False) | ||
|
||
Return the number of CPUs in the system. Returns ``None`` if undetermined. | ||
Return the number of logical 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))`` | ||
If *affinity* is true, return the number of logical CPUs the current process | ||
can use. If the :func:`sched_getaffinity` function is available, | ||
return ``len(os.sched_getaffinity(0))``. Otherwise, return | ||
``cpu_count(affinity=False)``. | ||
|
||
Linux control groups, *cgroups*, are not taken in account to get the number | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like people wanting such an API may also want cgroups to also be considered when the real question being answered is really "How parallel am I usefully allowed to be?". Does that need to be separated out into its own There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My PR just fix the documentation to avoid any misunderstading.
It is discussed in PR #80235. So far, nobody proposes any PR to implement this. Maybe this PR is a baby step forward :-) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even if we decide to support |
||
of logical CPUs. | ||
|
||
.. versionchanged:: 3.13 | ||
Add *affinity* parameter. | ||
|
||
.. versionadded:: 3.4 | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Add *affinity* parameter to :func:`os.cpu_count` to get the number of CPUs the | ||
current process can use. Patch by Victor Stinner. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have to update the documentation that it will return the number of logical CPUs for the process if the usable is True.
Because the current os.cpu_count returns the available CPU counts from the system not the process.
It's different layer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The doc says:
It's not clear enough? Do you want to propose a different phrasing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is reasonable.
I'd add "this may be less than the number of logical cpus returned by affinity=False due to OS or container limitations imposed upon the process" to make it more clear why people should want to use the
affinity=True
argument.PS thanks for making it keyword only!
I do wish this API never used the term "cpu"... everything these days is really a "logical_core" and what that even means depends a lot on underlying infrastructure and platform that Python may not be able to introspect. Way too late for that though. :)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My PR adds "logical CPU" to the doc. In previous bug reports, I saw some confusion between physical CPU core, CPU packages, CPU threads, and logical CPUs.