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

Skip to content

Commit 58521fd

Browse files
crwilcoxpitrou
authored andcommitted
bpo-30581: Windows: os.cpu_count() returns wrong number of processors (#2934) (#3267)
* 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 ea76791 commit 58521fd

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
@@ -11028,9 +11028,22 @@ os_cpu_count_impl(PyObject *module)
1102811028
{
1102911029
int ncpu = 0;
1103011030
#ifdef MS_WINDOWS
11031-
SYSTEM_INFO sysinfo;
11032-
GetSystemInfo(&sysinfo);
11033-
ncpu = sysinfo.dwNumberOfProcessors;
11031+
/* Vista is supported and the GetMaximumProcessorCount API is Win7+
11032+
Need to fallback to Vista behavior if this call isn't present */
11033+
HINSTANCE hKernel32;
11034+
hKernel32 = GetModuleHandleW(L"KERNEL32");
11035+
11036+
static DWORD(CALLBACK *_GetMaximumProcessorCount)(WORD) = NULL;
11037+
*(FARPROC*)&_GetMaximumProcessorCount = GetProcAddress(hKernel32,
11038+
"GetMaximumProcessorCount");
11039+
if (_GetMaximumProcessorCount != NULL) {
11040+
ncpu = _GetMaximumProcessorCount(ALL_PROCESSOR_GROUPS);
11041+
}
11042+
else {
11043+
SYSTEM_INFO sysinfo;
11044+
GetSystemInfo(&sysinfo);
11045+
ncpu = sysinfo.dwNumberOfProcessors;
11046+
}
1103411047
#elif defined(__hpux)
1103511048
ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
1103611049
#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)

0 commit comments

Comments
 (0)