The current implementation of os.cpu_count() uses sysconf(_SC_NPROCESSORS_ONLN) if it's available, except on Windows it prefers WinAPI GetActiveProcessorCount() if it's available, and on HP-UX it uses mpctl().
#ifdef MS_WINDOWS
#ifdef MS_WINDOWS_DESKTOP
ncpu = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
#endif
#elif defined(__hpux)
ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
ncpu = sysconf(_SC_NPROCESSORS_ONLN);
#elif defined(__VXWORKS__)
ncpu = _Py_popcount32(vxCpuEnabledGet());
#elif defined(__DragonFly__) || \
defined(__OpenBSD__) || \
defined(__FreeBSD__) || \
defined(__NetBSD__) || \
defined(__APPLE__)
int mib[2];
size_t len = sizeof(ncpu);
mib[0] = CTL_HW;
mib[1] = HW_NCPU;
if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
ncpu = 0;
#endif
The comment in the documentation about the number of usable CPUs is with regard to the CPU affinity of the current process, which can limit the process to a subset of the available processors. Thus len(os.sched_getaffinity(0)) determines the number of active/online processors that the current process can actually use.
Python's standard library has no support for Windows in regard to processor affinity, but the API functions can be called using ctypes.
Prior to Windows 11, each process is assigned to a processor group, each of which has up to 64 logical processors. Systems that have 64 or fewer logical processors only have a single processor group. For a process to use all available processors on a system that has multiple processor groups, threads have to be manually assigned to a processor group. In this case, the processor affinity can no longer be managed for the entire process via GetProcessAffinityMask() and SetProcessAffinityMask(). Instead use GetThreadGroupAffinity(), SetThreadGroupAffinity(), and SetThreadAffinityMask().
Starting with Windows 11, each process has a primary processor group, which is preferred, but by default the system is free to run a thread on any processor in any processor group in order to better manage performance and power usage. In this case, the above-mentioned API functions operate on the primary processor group of a process or thread. Affinity across all processor groups in Windows 11 is configured using "CPU set" functions such as GetProcessDefaultCpuSetMasks(), SetProcessDefaultCpuSetMasks(), GetThreadSelectedCpuSetMasks(), and SetThreadSelectedCpuSetMasks().