使用 Python Windows 获取 CPU 和 GPU 温度

2023-12-12

我想知道是否有办法在 python 中获取 CPU 和 GPU 温度。我已经找到了Linux的方法(使用psutil.sensors_temperature()),我想找到一种适用于 Windows 的方法。

一种查找 Mac OS 温度的方法也将受到赞赏,但我主要想要一种针对 Windows 的方法。

我更喜欢只使用 python 模块,但是 DLL 和 C/C++ 扩展也是完全可以接受的!

当我尝试执行以下操作时,我没有得到任何结果:

import wmi
w = wmi.WMI()
prin(w.Win32_TemperatureProbe()[0].CurrentReading)

当我尝试执行以下操作时,出现错误:

import wmi
w = wmi.WMI(namespace="root\wmi")
temperature_info = w.MSAcpi_ThermalZoneTemperature()[0]
print(temperature_info.CurrentTemperature)

Error:

wmi.x_wmi: <x_wmi: Unexpected COM Error (-2147217396, 'OLE error 0x8004100c', None, None)>

我听说过 OpenHardwareMoniter,但这需要我安装一些不是 python 模块的东西。我也希望不必以管理员身份运行脚本来获取结果。

我也可以使用 python 运行 Windows cmd 命令,但我还没有找到返回 CPU 温度的命令。

更新:我发现了这个:https://stackoverflow.com/a/58924992/13710015。 我不知道如何使用它。 当我尝试这样做时:print(OUTPUT_temp._fields_), I got

[('Board Temp', <class 'ctypes.c_ulong'>), ('CPU Temp', <class 'ctypes.c_ulong'>), ('Board Temp2', <class 'ctypes.c_ulong'>), ('temp4', <class 'ctypes.c_ulong'>), ('temp5', <class 'ctypes.c_ulong'>)]

注意:我真的不想以管理员身份运行它。如果我绝对必须这样做,我可以,但我宁愿不这样做。


我认为没有直接的方法可以实现这一目标。一些CPU生产商不提供wmi让您直接了解温度。

你可以使用OpenHardwareMoniter.dll。使用动态库。

首先,下载OpenHardwareMoniter。它包含一个名为OpenHardwareMonitorLib.dll(版本 0.9.6,2020 年 12 月)。

安装模块pythonnet:

pip install pythonnet

下面的代码在我的电脑上运行良好(获取CPU温度):

import clr # the pythonnet module.
clr.AddReference(r'YourdllPath') 
# e.g. clr.AddReference(r'OpenHardwareMonitor/OpenHardwareMonitorLib'), without .dll

from OpenHardwareMonitor.Hardware import Computer

c = Computer()
c.CPUEnabled = True # get the Info about CPU
c.GPUEnabled = True # get the Info about GPU
c.Open()
while True:
    for a in range(0, len(c.Hardware[0].Sensors)):
        # print(c.Hardware[0].Sensors[a].Identifier)
        if "/temperature" in str(c.Hardware[0].Sensors[a].Identifier):
            print(c.Hardware[0].Sensors[a].get_Value())
            c.Hardware[0].Update()

要获取 GPU 温度,请更改c.Hardware[0] to c.Hardware[1].

将结果与 进行比较:

enter image description here

enter image description here

注意:如果要获取CPU温度,需要以管理员身份运行。如果没有,您只会获得以下值Load。对于GPU温度,它可以在没有管理员权限的情况下工作(如在Windows 10 21H1上)。

我做了一些改变中文博客

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Python Windows 获取 CPU 和 GPU 温度 的相关文章