python - 使用 Python Windows 获取 CPU 和 GPU 温度

标签 python python-3.x windows windows-10

我想知道是否有办法在 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 temp 的命令。
更新:我发现了这个: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'>)]
注意:我真的不想以管理员身份运行它。如果我必须这样做,我可以,但我不想这样做。

最佳答案

我认为没有直接的方法可以实现这一目标。一些 CPU 生产商不会提供 wmi让您直接知道温度。
您可以使用 OpenHardwareMoniter.dll .使用动态库。
首先,下载 OpenHardwareMoniter .它包含一个名为 OpenHardwareMonitorLib.dll 的文件。 (版本 0.9.6,2020 年 12 月)。
安装模块pythonnet :

pip install pythonnet
下面的代码在我的 PC 上运行良好(获取 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]c.Hardware[1] .
将结果与:
enter image description here
enter image description here
注意:如果要获取 CPU 温度,需要以管理员身份运行。如果没有,你只会得到 Load 的值.对于 GPU 温度,它可以在没有管理员权限的情况下工作(如在 Windows 10 21H1 上)。
我对 Chinese Blog 做了一些更改

关于python - 使用 Python Windows 获取 CPU 和 GPU 温度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62617789/

相关文章:

C#检测光标是否隐藏

python - 使用 sharey=<other_axis> 删除子图上的 y 轴刻度标签

python-3.x - 将椭圆拟合到一组二维点

python - 一行作业与多行作业有什么区别

c# - 具有折叠/展开功能的控件 TreeView

windows - Windows : how to read Unicode input from console? 上的 GHCi

Python:获取每个字母/数字/等的更好方法。从字符串并将其转换为列表?

python - 如何从 IPython 中安装 Python 包?

python - 如何在没有相应值的列表中添加 None ?

Python:如何使用 OpenCV 在单击时从网络摄像头捕获图像