使用 Python Windows 获取 CPU 和 GPU Temp

Ket*_*mer 3 python windows python-3.x windows-10

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

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

我更喜欢只使用可通过 pip 下载的 python 模块,但我可以下载额外的东西。如果您的答案需要额外的文件,我希望它不需要太多文件。

当我尝试执行以下操作时,我得到 None:

import wmi
w = wmi.WMI()
prin(w.Win32_TemperatureProbe()[0].CurrentReading)
Run Code Online (Sandbox Code Playgroud)

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

import wmi
w = wmi.WMI(namespace="root\wmi")
temperature_info = w.MSAcpi_ThermalZoneTemperature()[0]
print(temperature_info.CurrentTemperature)
Run Code Online (Sandbox Code Playgroud)

错误:

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

我听说过 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'>)]
Run Code Online (Sandbox Code Playgroud)

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

编辑:我也可以在 Python 中使用 C/C++ 扩展,或者以任何方式在 python 中使用 CPU 温度。

Ges*_*hen 8

我发现了一个非常好的模块来获取NVIDIA GPU的温度。

pip 安装 gputil

打印温度的代码

import GPUtil
gpu = GPUtil.getGPUs()[0]
print(gpu.temperature)
Run Code Online (Sandbox Code Playgroud)

在这里找到的


jiz*_*AMA 7

我认为没有直接的方法来实现这一点。一些 CPU 生产商不会wmi直接让你知道温度。

您可以使用OpenHardwareMoniter.dll.Use 动态库。

首先,下载OpenHardwareMoniter.It 包含一个名为OpenHardwareMoniter.dll.

安装模块pythonnet

pip install pythonnet
Run Code Online (Sandbox Code Playgroud)

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

import clr # the pythonnet module.
clr.AddReference(r'YourdllPath')
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 "/intelcpu/0/temperature" in str(c.Hardware[0].Sensors[a].Identifier):
            print(c.Hardware[0].Sensors[a].get_Value())
            c.Hardware[0].Update()
Run Code Online (Sandbox Code Playgroud)

要获得GPU温度,改c.Hardware[0]c.Hardware[1]

将结果与:

在此处输入图片说明

在此处输入图片说明

注意:如果要获取温度,需要以管理员身份运行。如果没有,您将只获得 的值Load

我从一个中文博客做了一些改动