5

我编写了可在 Windows XP 和 Windows Server 2008 64 位上运行的代码。但是我刚刚启动了一个 Amazon Windows 64 位实例并且代码失败了。

很简单,看起来像这样

import multiprocessing

processors = multiprocessing.cpu_count()
print processors

我收到一个我不理解的 NotImplementedError 并且文档对解释没有帮助。

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

还有其他人遇到这个问题/错误吗?

4

3 回答 3

4

它可能只是多处理模块,尝试使用它可能工作的psutil模块。所以在你的情况下,只需这样做:

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

我在 Amazon Windows 64 位上试过这个,效果很好。

于 2012-11-24T19:43:08.800 回答
3

如果您只需要获取 CPU 计数psutil,则可以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

编辑

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
于 2012-11-25T09:54:21.343 回答
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()
于 2014-06-25T13:29:42.383 回答