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

Skip to content

Commit c67bae0

Browse files
crwilcoxpitrou
authored andcommitted
bpo-30581: Windows: os.cpu_count() returns wrong number of processors (#2934)
* Fixes #30581 by adding a path to use newer GetMaximumProcessorCount API on Windows calls to os.cpu_count() * Add NEWS.d entry for bpo-30581, os.cpu_count on Windows. * Tweak NEWS entry
1 parent 390eadd commit c67bae0

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
os.cpu_count() now returns the correct number of processors on Windows
2+
when the number of logical processors is greater than 64.

Modules/posixmodule.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11174,9 +11174,22 @@ os_cpu_count_impl(PyObject *module)
1117411174
{
1117511175
int ncpu = 0;
1117611176
#ifdef MS_WINDOWS
11177-
SYSTEM_INFO sysinfo;
11178-
GetSystemInfo(&sysinfo);
11179-
ncpu = sysinfo.dwNumberOfProcessors;
11177+
/* Vista is supported and the GetMaximumProcessorCount API is Win7+
11178+
Need to fallback to Vista behavior if this call isn't present */
11179+
HINSTANCE hKernel32;
11180+
hKernel32 = GetModuleHandleW(L"KERNEL32");
11181+
11182+
static DWORD(CALLBACK *_GetMaximumProcessorCount)(WORD) = NULL;
11183+
*(FARPROC*)&_GetMaximumProcessorCount = GetProcAddress(hKernel32,
11184+
"GetMaximumProcessorCount");
11185+
if (_GetMaximumProcessorCount != NULL) {
11186+
ncpu = _GetMaximumProcessorCount(ALL_PROCESSOR_GROUPS);
11187+
}
11188+
else {
11189+
SYSTEM_INFO sysinfo;
11190+
GetSystemInfo(&sysinfo);
11191+
ncpu = sysinfo.dwNumberOfProcessors;
11192+
}
1118011193
#elif defined(__hpux)
1118111194
ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
1118211195
#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)

0 commit comments

Comments
 (0)