繁体   English   中英

使用 Python Windows 获取 CPU 和 GPU 温度

[英]Get CPU and GPU Temp using Python Windows

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

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

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

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

首先,下载OpenHardwareMoniter 。它包含一个名为OpenHardwareMoniter.dll的文件。

安装模块pythonnet

pip install pythonnet

下面的代码在我的 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()

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

将结果与:

在此处输入图像描述

在此处输入图像描述

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

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

我找到了一个很好的模块来获取NVIDIA GPU 的温度。

pip 安装 gputil

打印温度的代码

import GPUtil
gpu = GPUtil.getGPUs()[0]
print(gpu.temperature)

我在这里找到了

所以你可以使用gpiozero先获取温度pip install gpiozerofrom gpiozero import CPUTemperature导入它,然后cpu = CPUTemperature() print(cpu.temperature)

代码:

from gpiozero import CPUTemperature

cpu = CPUTemperature()
print(cpu.temperature)

希望你喜欢。 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM