Python CPU Count在一台Windows服务器上运行但在另一台服务器上运行?

时间:2012-11-24 19:34:35

标签: python multiprocessing cpu

我写的代码已经在Windows XP和Windows Server 2008 64位上运行。但是,我刚刚启动了一个亚马逊Windows 64位实例,但代码失败了。

非常简单,看起来像这样

import multiprocessing

processors = multiprocessing.cpu_count()
print processors

我收到一个NotImplementedError,我不明白,而且文档对解释没有帮助。

我只是不明白为什么它可以在一台服务器上运行,而不是在另一台服务器上运行相同的Python 2.7

其他人遇到此问题/错误?

3 个答案:

答案 0 :(得分:4)

它可能只是多处理模块,尝试使用它可能有效的psutil模块。在你的情况下只做:

import psutil
processors = psutil.cpu_count()
print processors
>>> 4

我在亚马逊Windows 64位上试过这个并且效果很好。

答案 1 :(得分:3)

如果您需要使用psutil获取CPU数量,则可以改为使用ctypes

import ctypes
from ctypes import wintypes

class SYSTEM_INFO(ctypes.Structure):
    _fields_ = [
        ('wProcessorArchitecture', wintypes.WORD),
        ('wReserved', wintypes.WORD),
        ('dwPageSize', wintypes.DWORD),
        ('lpMinimumApplicationAddress', wintypes.LPVOID),
        ('lpMaximumApplicationAddress', wintypes.LPVOID),
        ('dwActiveProcessorMask', ctypes.c_size_t),
        ('dwNumberOfProcessors', wintypes.DWORD),
        ('dwProcessorType', wintypes.DWORD),
        ('dwAllocationGranularity', wintypes.DWORD),
        ('wProcessorLevel', wintypes.WORD),
        ('wProcessorRevision', wintypes.WORD),
    ]

GetSystemInfo = ctypes.windll.kernel32.GetSystemInfo
GetSystemInfo.restype = None
GetSystemInfo.argtypes = [ctypes.POINTER(SYSTEM_INFO)]

def cpu_count():
    sysinfo = SYSTEM_INFO()
    GetSystemInfo(sysinfo)
    num = sysinfo.dwNumberOfProcessors
    if num == 0:
        raise NotImplementedError('cannot determine number of cpus')
    return num

修改

这是try的替代方法,它可能返回与NUMBER_OF_PROCESSORS环境变量相同的值。请注意,文档说使用GetSystemInfo代替,这是psutil使用的。这也是使用本机NT API,通常不鼓励。

import ctypes
from ctypes import wintypes

SystemBasicInformation = 0

class SYSTEM_INFORMATION(ctypes.Structure): pass
PSYSTEM_INFORMATION = ctypes.POINTER(SYSTEM_INFORMATION)

class SYSTEM_BASIC_INFORMATION(SYSTEM_INFORMATION):
    _fields_ = [
        ('Reserved1', wintypes.BYTE * 24),
        ('Reserved2', wintypes.LPVOID * 4),
        ('NumberOfProcessors', ctypes.c_ubyte),
    ]

ntdll = ctypes.windll.ntdll
NtQuerySystemInformation = ntdll.NtQuerySystemInformation
NtQuerySystemInformation.argtypes = [
    wintypes.LONG,       # SystemInformationClass
    PSYSTEM_INFORMATION, # SystemInformation
    wintypes.ULONG,      # SystemInformationLength
    wintypes.PULONG]     # ReturnLength

def cpu_count():
    info = SYSTEM_BASIC_INFORMATION()
    retlen = wintypes.ULONG()
    status = NtQuerySystemInformation(SystemBasicInformation,
                                      info, 
                                      ctypes.sizeof(info), 
                                      retlen)
    num = info.NumberOfProcessors
    if status < 0 or num == 0:
        raise NotImplementedError('cannot determine number of cpus')
    return num

答案 2 :(得分:1)

失败是由于multiprocessing.cpu_count()依赖于Windows上的NUMBER_OF_PROCESSORS环境变量,偶尔会丢失。使用wmi作为替代品。感谢您提出建议eryksun

if sys.platform == 'win32':
    import wmi
    c = wmi.WMI(find_classes=False)
    return sum(x.NumberOfLogicalProcessors for x in c.Win32_Processor())
else:
    return multiprocessing.cpu_count()