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

Skip to content

Commit 920fb83

Browse files
committed
gh-109595: Update -Xcpu_count=<n> cmdline to support process mode
1 parent f5edb56 commit 920fb83

File tree

6 files changed

+29
-3
lines changed

6 files changed

+29
-3
lines changed

Doc/c-api/init_config.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@ PyConfig
884884
override the return values of :func:`os.cpu_count`,
885885
:func:`os.process_cpu_count`, and :func:`multiprocessing.cpu_count`.
886886
887-
Configured by the :samp:`-X cpu_count={n|default}` command line
887+
Configured by the :samp:`-X cpu_count={n|default|process}` command line
888888
flag or the :envvar:`PYTHON_CPU_COUNT` environment variable.
889889
890890
Default: ``-1``.

Doc/using/cmdline.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,9 @@ Miscellaneous options
551551
*n* must be greater than or equal to 1.
552552
This option may be useful for users who need to limit CPU resources of a
553553
container system. See also :envvar:`PYTHON_CPU_COUNT`.
554-
If *n* is ``default``, nothing is overridden.
554+
If *n* is ``default``, nothing is overridden, if *n* is ``process``,
555+
:func:`os.os.process_cpu_count` will be alias of :func:`os.cpu_count`
556+
and :func:`multiprocessing.cpu_count`.
555557

556558
It also allows passing arbitrary values and retrieving them through the
557559
:data:`sys._xoptions` dictionary.
@@ -1076,6 +1078,8 @@ conflict.
10761078

10771079
If this variable is set to a positive integer, it overrides the return
10781080
values of :func:`os.cpu_count` and :func:`os.process_cpu_count`.
1081+
if this variable is set to ``process``, :func:`os.process_cpu_count` becomes
1082+
alias of :func:`os.cpu_count` and :func:`multiprocessing.cpu_count`.
10791083

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

Lib/os.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,9 @@ def process_cpu_count():
11471147
current process. Return None if indeterminable.
11481148
"""
11491149
return len(sched_getaffinity(0))
1150+
if sys._get_cpu_count_config() == -2:
1151+
# Just an alias to process_count() (same docstring)
1152+
cpu_count = process_cpu_count
11501153
else:
11511154
# Just an alias to cpu_count() (same docstring)
11521155
process_cpu_count = cpu_count

Lib/test/test_cmd_line.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,15 @@ def test_cpu_count_default(self):
915915
es = assert_python_ok('-c', code, PYTHON_CPU_COUNT='default')
916916
self.assertEqual(self.res2int(res), (os.cpu_count(), os.process_cpu_count()))
917917

918+
def test_cpu_count_process(self):
919+
code = "import os; print(os.cpu_count(), os.process_cpu_count())"
920+
res = assert_python_ok('-X', 'cpu_count=process', '-c', code)
921+
self.assertEqual(self.res2int(res), (os.process_cpu_count(), os.process_cpu_count()))
922+
res = assert_python_ok('-X', 'cpu_count=default', '-c', code, PYTHON_CPU_COUNT='process')
923+
self.assertEqual(self.res2int(res), (os.cpu_count(), os.process_cpu_count()))
924+
es = assert_python_ok('-c', code, PYTHON_CPU_COUNT='process')
925+
self.assertEqual(self.res2int(res), (os.process_cpu_count(), os.process_cpu_count()))
926+
918927
def res2int(self, res):
919928
out = res.out.strip().decode("utf-8")
920929
return tuple(int(i) for i in out.split())
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Update :option:`-X cpu_count <-X>` command line option to support
2+
``process`` mode that :func:`os.process_cpu_count` becomes an alias of
3+
:func:`os.cpu_count` and :func:`multiprocessing.cpu_count`. Patch by Donghee
4+
Na.

Python/initconfig.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ The following implementation-specific options are available:\n\
232232
This helps avoid denial of service attacks when parsing untrusted data.\n\
233233
The default is sys.int_info.default_max_str_digits. 0 disables.\n\
234234
\n\
235-
-X cpu_count=[n|default]: Override the return value of os.cpu_count(),\n\
235+
-X cpu_count=[n|default|process]: Override the return value of os.cpu_count(),\n\
236236
os.process_cpu_count(), and multiprocessing.cpu_count(). This can help users who need\n\
237237
to limit resources in a container."
238238

@@ -1636,6 +1636,9 @@ config_init_cpu_count(PyConfig *config)
16361636
if (strcmp(env, "default") == 0) {
16371637
cpu_count = -1;
16381638
}
1639+
else if(strcmp(env, "process") == 0) {
1640+
cpu_count = -2;
1641+
}
16391642
else if (_Py_str_to_int(env, &cpu_count) < 0 || cpu_count < 1) {
16401643
goto error;
16411644
}
@@ -1650,6 +1653,9 @@ config_init_cpu_count(PyConfig *config)
16501653
if (wcscmp(sep + 1, L"default") == 0) {
16511654
cpu_count = -1;
16521655
}
1656+
else if (wcscmp(sep + 1, L"process") == 0) {
1657+
cpu_count = -2;
1658+
}
16531659
else if (config_wstr_to_int(sep + 1, &cpu_count) < 0 || cpu_count < 1) {
16541660
goto error;
16551661
}

0 commit comments

Comments
 (0)