使用 Python Windows 获取 CPU 和 GPU 温度

我想知道是否有办法在 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)

错误:


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_)时,我得到了


[('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'>)]

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


蓝山帝景
浏览 929回答 3
3回答

波斯汪

我认为没有直接的方法可以实现这一目标。一些 CPU 生产商不会wmi直接让您知道温度。你可以使用OpenHardwareMoniter.dll.&nbsp;使用动态库。首先,下载OpenHardwareMoniter.&nbsp;它包含一个名为OpenHardwareMonitorLib.dll(版本 0.9.6,2020 年 12 月)的文件。安装模块pythonnet:pip&nbsp;install&nbsp;pythonnet下面的代码在我的电脑上运行良好(获取 CPU 温度):import clr # the pythonnet module.clr.AddReference(r'YourdllPath')&nbsp;# e.g. clr.AddReference(r'OpenHardwareMonitor/OpenHardwareMonitorLib'), without .dllfrom OpenHardwareMonitor.Hardware import Computerc = Computer()c.CPUEnabled = True # get the Info about CPUc.GPUEnabled = True # get the Info about GPUc.Open()while True:&nbsp; &nbsp; for a in range(0, len(c.Hardware[0].Sensors)):&nbsp; &nbsp; &nbsp; &nbsp; # print(c.Hardware[0].Sensors[a].Identifier)&nbsp; &nbsp; &nbsp; &nbsp; if "/temperature" in str(c.Hardware[0].Sensors[a].Identifier):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(c.Hardware[0].Sensors[a].get_Value())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.Hardware[0].Update()要获取 GPU 温度,请将 更改c.Hardware[0]为c.Hardware[1]。将结果与:

牧羊人nacy

我找到了一个非常好的模块来获取NVIDIA&nbsp;GPU 的温度。pip 安装 gputil打印温度的代码import&nbsp;GPUtil gpu&nbsp;=&nbsp;GPUtil.getGPUs()[0] print(gpu.temperature)我在这里找到了

慕妹3146593

所以你可以gpiozero先获取温度pip install gpiozero然后from gpiozero import CPUTemperature导入它cpu = CPUTemperature() print(cpu.temperature)代码:from gpiozero import CPUTemperaturecpu = CPUTemperature()print(cpu.temperature)希望你喜欢。:)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python