Skip to content

Commit aa92927

Browse files
zoobazware
authored andcommitted
bpo-33166: Change os.cpu_count to return active (real) processors (GH-15949)
1 parent e20134f commit aa92927

File tree

2 files changed

+5
-17
lines changed

2 files changed

+5
-17
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:func:`os.cpu_count` now returns active processors rather than maximum
2+
processors.

Modules/posixmodule.c

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12204,23 +12204,9 @@ os_cpu_count_impl(PyObject *module)
1220412204
{
1220512205
int ncpu = 0;
1220612206
#ifdef MS_WINDOWS
12207-
/* Vista is supported and the GetMaximumProcessorCount API is Win7+
12208-
Need to fallback to Vista behavior if this call isn't present */
12209-
HINSTANCE hKernel32;
12210-
static DWORD(CALLBACK *_GetMaximumProcessorCount)(WORD) = NULL;
12211-
Py_BEGIN_ALLOW_THREADS
12212-
hKernel32 = GetModuleHandleW(L"KERNEL32");
12213-
*(FARPROC*)&_GetMaximumProcessorCount = GetProcAddress(hKernel32,
12214-
"GetMaximumProcessorCount");
12215-
Py_END_ALLOW_THREADS
12216-
if (_GetMaximumProcessorCount != NULL) {
12217-
ncpu = _GetMaximumProcessorCount(ALL_PROCESSOR_GROUPS);
12218-
}
12219-
else {
12220-
SYSTEM_INFO sysinfo;
12221-
GetSystemInfo(&sysinfo);
12222-
ncpu = sysinfo.dwNumberOfProcessors;
12223-
}
12207+
/* Declare prototype here to avoid pulling in all of the Win7 APIs in 3.8 */
12208+
DWORD WINAPI GetActiveProcessorCount(WORD group);
12209+
ncpu = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
1222412210
#elif defined(__hpux)
1222512211
ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
1222612212
#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)

0 commit comments

Comments
 (0)