-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
gh-109595: Add -Xcpu_count=<n> cmdline for container users #109667
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
Conversation
|
cc @indygreg |
|
Please read the issue of why this flag is needed and see also actual use-case from #109595 (comment) Even if the container system is important these days, there is no way to limit CPU resources for the container environment from Python program side. |
Co-authored-by: Victor Stinner <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
Programs/_testembed.c
Outdated
|
|
||
| putenv("PYTHONINTMAXSTRDIGITS=6666"); | ||
| config.int_max_str_digits = 31337; | ||
| config.cpu_count = -1; |
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.
Can you please set a more interesting value like 1234?
vstinner
left a comment
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.
Can you please document the change in Doc/whatsnew/3.13.rst?
Doc/using/cmdline.rst
Outdated
| * :samp:`-X cpu_count={n}` will override the number of CPU count from system. | ||
| If this option is provided, :func:`os.cpu_count` will return the overrided value. | ||
| And *n* must be greater equal then 1. |
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.
| * :samp:`-X cpu_count={n}` will override the number of CPU count from system. | |
| If this option is provided, :func:`os.cpu_count` will return the overrided value. | |
| And *n* must be greater equal then 1. | |
| * :samp:`-X cpu_count={n}` overrides the number of CPU count from system: | |
| :func:`os.cpu_count` returns *n*. The value *n* must be greater than | |
| or equal to 1. |
Misc/NEWS.d/next/Core and Builtins/2023-09-22-01-44-53.gh-issue-109595.fVINgD.rst
Outdated
Show resolved
Hide resolved
Python/initconfig.c
Outdated
| static PyStatus | ||
| config_init_cpu_count(PyConfig *config) | ||
| { | ||
| int cpu_count = -1; |
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 can move the variable declaration inside the "if (sep)" block. Does it have to be initialized?
If you are afraid of undefined behavior, maybe config_wstr_to_int() should set *result to 0 on error.
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.
Now I added PYTHONCPUCOUNT envvar, so the current structure will be needed.
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.
If you want, you can move the variable declaration inside each "if (env)" block and duplicate the variable, to better show its scope. But well, that's just a personal preference. Feel free to ignore my coding style remark ;-)
Co-authored-by: Victor Stinner <[email protected]>
Co-authored-by: Victor Stinner <[email protected]>
Adds PYTHON_CPU_COUNT help text. Mentions multiprocessing.cpu_count. rewords a few statements.
|
The majority wants PYTHON_CPU_COUNT env name: https://discuss.python.org/t/change-environment-variable-style/35180 |
vstinner
left a comment
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.
LGTM. I see that you addressed the two @gpshead's comments. You can merge.
|
Congrats. |
|
🎉 thanks everybody! |
|
Follow-up: issue #110649 to add -X cpu_count=process. |
…hon#109667) --------- Co-authored-by: Victor Stinner <[email protected]> Co-authored-by: Gregory P. Smith [Google LLC] <[email protected]>
|
Hello, Just a ping to say that the function doesn't work and the doc is wrong.
expected: It seems the value is only read once at interpreter startup. FYI: This is a problem because python cannot detect the number cores correctly. It's not aware of the number of cpu exposed to containers/VM and try to use all CPU from the host machine, it's not aware of the number of CPU it should use in compute clusters that expect 1 core per job. I was looking forward to setting |
You're correct: the If you really have to change But it's better to set the environment variable before Python startup. |
I agree but it's not possible in practice.
So the fix for me is to fix the CPU detection in the For reference, this is a pretty critical issue with the last generation of server CPUs released in 2024 which have way more cores (up to 192c/384t per socket). It only take a few misbehaved apps/libraries each trying to create N subthreads/subprocesses to accidentally kill the server and other jobs. 64x64 on an older server is merely 4096 threads/processes. 512x512 on a recent server is 262144 threads/processes. |
|
This closed issue is not the right place to ask questions on how to use Python. I suggest you finding another place to ask your question such as the Python Help category of discuss.python.org. |
|
This thread is the right place to discuss cpu detection in the interpreter, as it was modified by this ticket and needs further improvements.
|
You can propose a PR, I don't know if it would be accepted. @corona10 added this feature.
I don't think that it's a good idea. It's common in Python to only read an environment variable once at startup.
So far, the consensus is to not read cgroup limits since there is not reliable way to get a number of CPUs. Again, this closed issue is not the right place to discuss it. |
|
You can propose a PR for doc. But rest of things does not look like good idea. |
|
morotti please open a thread on discuss.python.org ideally which could ultimately spawn new issues. closed PRs are not a meaningful place for discussions. |
📚 Documentation preview 📚: https://cpython-previews--109667.org.readthedocs.build/