Skip to content

Commit 43ee0e2

Browse files
bpo-33166: Change os.cpu_count to return active (real) processors (GH-15949)
(cherry picked from commit aa92927) Co-authored-by: Steve Dower <steve.dower@python.org>
1 parent 63eefc3 commit 43ee0e2

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
@@ -12176,23 +12176,9 @@ os_cpu_count_impl(PyObject *module)
1217612176
{
1217712177
int ncpu = 0;
1217812178
#ifdef MS_WINDOWS
12179-
/* Vista is supported and the GetMaximumProcessorCount API is Win7+
12180-
Need to fallback to Vista behavior if this call isn't present */
12181-
HINSTANCE hKernel32;
12182-
static DWORD(CALLBACK *_GetMaximumProcessorCount)(WORD) = NULL;
12183-
Py_BEGIN_ALLOW_THREADS
12184-
hKernel32 = GetModuleHandleW(L"KERNEL32");
12185-
*(FARPROC*)&_GetMaximumProcessorCount = GetProcAddress(hKernel32,
12186-
"GetMaximumProcessorCount");
12187-
Py_END_ALLOW_THREADS
12188-
if (_GetMaximumProcessorCount != NULL) {
12189-
ncpu = _GetMaximumProcessorCount(ALL_PROCESSOR_GROUPS);
12190-
}
12191-
else {
12192-
SYSTEM_INFO sysinfo;
12193-
GetSystemInfo(&sysinfo);
12194-
ncpu = sysinfo.dwNumberOfProcessors;
12195-
}
12179+
/* Declare prototype here to avoid pulling in all of the Win7 APIs in 3.8 */
12180+
DWORD WINAPI GetActiveProcessorCount(WORD group);
12181+
ncpu = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
1219612182
#elif defined(__hpux)
1219712183
ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
1219812184
#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)

0 commit comments

Comments
 (0)