【问题标题】:Python CPU Count works on one windows server but not on another?Python CPU Count 可以在一台 Windows 服务器上工作,但不能在另一台服务器上工作?
【发布时间】:2017-01-24 13:11:52
【问题描述】:

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

看起来很简单

import multiprocessing

processors = multiprocessing.cpu_count()
print processors

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

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

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

【问题讨论】:

  • 是否需要使用多处理模块?
  • 对于 win32 cpu_count 使用 num = int(os.environ['NUMBER_OF_PROCESSORS'])。如果未定义环境变量,它将引发 NotImplementedError。

标签: python multiprocessing cpu


【解决方案1】:

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

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

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

【讨论】:

  • 嗨,enginefree - 可以,另一个要安装的模块,但希望它比通过多处理更可靠。非常感谢
  • 很高兴它有帮助,我的几台服务器也遇到了同样的问题。
  • 这已被替换为psutil.cpu_count() - github.com/giampaolo/psutil/blob/master/HISTORY.rst
【解决方案2】:

如果您只需要使用 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

编辑:

这里有一个替代方法可以尝试,它可能返回与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

【讨论】:

  • 不是很便携,但为不同的方法 +1。
  • @BurhanKhalid,如果sys.platform == 'win32' 和multiprocessing.cpu_count 引发NotImplementedError,我会将其用作后备。
  • 当我在我的 Windows 机器上运行它时,我的 CPU 计数为 9。当我运行 multiprocessing.cpu_count()(或os.getenv("NUMBER_OF_PROCESSORS"))时,我的 CPU 计数为 8。谁能解释一下?
  • 您也可以使用 WMI 检查:wmic cpu get NumberOfCores, NumberOfLogicalProcessors。
【解决方案3】:

失败是由于 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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-09
    • 2015-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多