Sorry, something went wrong.
|
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. |
Sorry, something went wrong.
|
|
||
| putenv("PYTHONINTMAXSTRDIGITS=6666"); | ||
| config.int_max_str_digits = 31337; | ||
| config.cpu_count = -1; |
There was a problem hiding this comment.
Can you please set a more interesting value like 1234?
Sorry, something went wrong.
There was a problem hiding this comment.
Can you please document the change in Doc/whatsnew/3.13.rst?
Sorry, something went wrong.
| * :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.
| * :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. |
Sorry, something went wrong.
| static PyStatus | ||
| config_init_cpu_count(PyConfig *config) | ||
| { | ||
| int cpu_count = -1; |
There was a problem hiding this comment.
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.
Sorry, something went wrong.
There was a problem hiding this comment.
Now I added PYTHONCPUCOUNT envvar, so the current structure will be needed.
Sorry, something went wrong.
There was a problem hiding this comment.
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 ;-)
Sorry, something went wrong.
|
The majority wants PYTHON_CPU_COUNT env name: https://discuss.python.org/t/change-environment-variable-style/35180 |
Sorry, something went wrong.
|
@gpshead I will merge this PR by tomorrow. Please let me know if there needs more change. :) |
Sorry, something went wrong.
|
Congrats. |
Sorry, something went wrong.
|
🎉 thanks everybody! |
Sorry, something went wrong.
Sorry, something went wrong.
|
Follow-up: issue #110649 to add -X cpu_count=process. |
Sorry, something went wrong.
|
Hello, Just a ping to say that the function doesn't work and the doc is wrong. Changed in version 3.13: If -X cpu_count is given or PYTHON_CPU_COUNT is set, cpu_count() returns the overridden value n. >>> sys.version_info
sys.version_info(major=3, minor=14, micro=0, releaselevel='final', serial=0)
>>> os.cpu_count()
48
>>> os.process_cpu_count()
47
>>> os.environ["PYTHON_CPU_COUNT"] = "1"
>>> os.cpu_count()
48
>>> os.process_cpu_count()
47
expected: PYTHON_CPU_COUNT should change the return value of os.cpu_count on python 3.13+ as explained by the doc. 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 PYTHON_CPU_COUNT on interpreter startup to finally fix the issue in a simple consistent way, but it doesn't work ^^ |
Sorry, something went wrong.
|
expected: PYTHON_CPU_COUNT should change the return value of os.cpu_count on python 3.13+ as explained by the doc. You're correct: the PYTHON_CPU_COUNT environment variable is only read once at startup, not during execution. If you really have to change os.cpu_count() return value at runtime, you can mock the function: $ python
>>> import os
>>> os.cpu_count()
12
>>> os.cpu_count=lambda:1
>>> os.cpu_count()
1
But it's better to set the environment variable before Python startup. |
Sorry, something went wrong.
|
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 sitecustomize.py and configure environment variables for all the known frameworks: OMP_NUM_THREADS, OPENBLAS_NUM_THREADS, POLARS_MAX_THREADS, PYTHON_CPU_COUNT etc... 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. |
Sorry, something went wrong.
|
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. |
Sorry, something went wrong.
|
This thread is the right place to discuss cpu detection in the interpreter, as it was modified by this ticket and needs further improvements.
|
Sorry, something went wrong.
|
do you mind if I send a patch to the doc, to mention that the variable is only read once at startup? You can propose a PR, I don't know if it would be accepted. @corona10 added this feature. do you mind if we correct the code to actually respect the variable at runtime? I don't think that it's a good idea. It's common in Python to only read an environment variable once at startup. do you mind if we correct these functions to return the correct number of CPU, taking into account cgroup limits. 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. |
Sorry, something went wrong.
|
You can propose a PR for doc. But rest of things does not look like good idea. |
Sorry, something went wrong.
|
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. |
Sorry, something went wrong.
📚 Documentation preview 📚: https://cpython-previews--109667.org.readthedocs.build/