Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-110649: Update -Xcpu_count=<n> cmdline to support process mode #110639

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Doc/c-api/init_config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ PyConfig
override the return values of :func:`os.cpu_count`,
:func:`os.process_cpu_count`, and :func:`multiprocessing.cpu_count`.

Configured by the :samp:`-X cpu_count={n|default}` command line
Configured by the :samp:`-X cpu_count={n|default|process}` command line
flag or the :envvar:`PYTHON_CPU_COUNT` environment variable.

Default: ``-1``.
Expand Down
5 changes: 4 additions & 1 deletion Doc/using/cmdline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,8 @@ Miscellaneous options
*n* must be greater than or equal to 1.
This option may be useful for users who need to limit CPU resources of a
container system. See also :envvar:`PYTHON_CPU_COUNT`.
If *n* is ``default``, nothing is overridden.
If *n* is ``default``, nothing is overridden. If *n* is ``process``,
:func:`os.cpu_count` becomes an alias of :func:`os.process_cpu_count`

It also allows passing arbitrary values and retrieving them through the
:data:`sys._xoptions` dictionary.
Expand Down Expand Up @@ -1076,6 +1077,8 @@ conflict.

If this variable is set to a positive integer, it overrides the return
values of :func:`os.cpu_count` and :func:`os.process_cpu_count`.
If this variable is set to ``process``, :func:`os.cpu_count` becomes
an alias of :func:`os.process_cpu_count`.

See also the :option:`-X cpu_count <-X>` command-line option.

Expand Down
2 changes: 1 addition & 1 deletion Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ os
the new environment variable :envvar:`PYTHON_CPU_COUNT` or the new command-line option
:option:`-X cpu_count <-X>`. This option is useful for users who need to limit
CPU resources of a container system without having to modify the container (application code).
(Contributed by Donghee Na in :gh:`109595`)
(Contributed by Donghee Na in :gh:`109595` and :gh:`110649`)

pathlib
-------
Expand Down
3 changes: 3 additions & 0 deletions Lib/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,9 @@ def process_cpu_count():
current process. Return None if indeterminable.
"""
return len(sched_getaffinity(0))
if sys._get_cpu_count_config() == -2:
# Just an alias to process_count() (same docstring)
cpu_count = process_cpu_count
else:
# Just an alias to cpu_count() (same docstring)
process_cpu_count = cpu_count
9 changes: 9 additions & 0 deletions Lib/test/test_cmd_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,15 @@ def test_cpu_count_default(self):
es = assert_python_ok('-c', code, PYTHON_CPU_COUNT='default')
self.assertEqual(self.res2int(res), (os.cpu_count(), os.process_cpu_count()))

def test_cpu_count_process(self):
code = "import os; print(os.cpu_count(), os.process_cpu_count())"
res = assert_python_ok('-X', 'cpu_count=process', '-c', code)
self.assertEqual(self.res2int(res), (os.process_cpu_count(), os.process_cpu_count()))
res = assert_python_ok('-X', 'cpu_count=default', '-c', code, PYTHON_CPU_COUNT='process')
self.assertEqual(self.res2int(res), (os.cpu_count(), os.process_cpu_count()))
es = assert_python_ok('-c', code, PYTHON_CPU_COUNT='process')
self.assertEqual(self.res2int(res), (os.process_cpu_count(), os.process_cpu_count()))

def res2int(self, res):
out = res.out.strip().decode("utf-8")
return tuple(int(i) for i in out.split())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Update :option:`-X cpu_count <-X>` command line option to support
``process`` mode that :func:`os.cpu_count` becomes an alias of
:func:`os.process_cpu_count`. Patch by Donghee Na.
8 changes: 7 additions & 1 deletion Python/initconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ The following implementation-specific options are available:\n\
This helps avoid denial of service attacks when parsing untrusted data.\n\
The default is sys.int_info.default_max_str_digits. 0 disables.\n\
\n\
-X cpu_count=[n|default]: Override the return value of os.cpu_count(),\n\
-X cpu_count=[n|default|process]: Override the return value of os.cpu_count(),\n\
os.process_cpu_count(), and multiprocessing.cpu_count(). This can help users who need\n\
to limit resources in a container."

Expand Down Expand Up @@ -1636,6 +1636,9 @@ config_init_cpu_count(PyConfig *config)
if (strcmp(env, "default") == 0) {
cpu_count = -1;
}
else if(strcmp(env, "process") == 0) {
cpu_count = -2;
}
else if (_Py_str_to_int(env, &cpu_count) < 0 || cpu_count < 1) {
goto error;
}
Expand All @@ -1650,6 +1653,9 @@ config_init_cpu_count(PyConfig *config)
if (wcscmp(sep + 1, L"default") == 0) {
cpu_count = -1;
}
else if (wcscmp(sep + 1, L"process") == 0) {
cpu_count = -2;
}
else if (config_wstr_to_int(sep + 1, &cpu_count) < 0 || cpu_count < 1) {
goto error;
}
Expand Down