为了账号安全,请及时绑定邮箱和手机立即绑定

使用 Python Windows 获取 CPU 和 GPU 温度

使用 Python Windows 获取 CPU 和 GPU 温度

蓝山帝景 2023-02-07 10:49:29
我想知道是否有办法在 python 中获取 CPU 和 GPU 温度。我已经找到了Linux的方法(使用psutil.sensors_temperature()),我想找到Windows的方法。一种查找 Mac OS 温度的方法也将不胜感激,但我主要想要一种用于 Windows 的方法。我更喜欢只使用 python 模块,但 DLL 和 C/C++ 扩展也完全可以接受!当我尝试执行以下操作时,我没有得到:import wmiw = wmi.WMI()prin(w.Win32_TemperatureProbe()[0].CurrentReading)当我尝试执行以下操作时,出现错误:import wmiw = 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'>)]注意:我真的不想以管理员身份运行它。如果我绝对必须这样做,我可以,但我不想这样做。
查看完整描述

3 回答

?
波斯汪

TA贡献1811条经验 获得超4个赞

我认为没有直接的方法可以实现这一目标。一些 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]为c.Hardware[1]。


将结果与:

//img1.sycdn.imooc.com//63e1bc610001472808600638.jpg

//img1.sycdn.imooc.com//63e1bc6a0001518f05680789.jpg

查看完整回答
反对 回复 2023-02-07
?
牧羊人nacy

TA贡献1862条经验 获得超7个赞

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

pip 安装 gputil

打印温度的代码

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

我在这里找到了


查看完整回答
反对 回复 2023-02-07
?
慕妹3146593

TA贡献1820条经验 获得超9个赞

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


代码:


from gpiozero import CPUTemperature


cpu = CPUTemperature()

print(cpu.temperature)

希望你喜欢。:)


查看完整回答
反对 回复 2023-02-07
  • 3 回答
  • 0 关注
  • 911 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信