亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 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]。


將結果與:

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

http://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 關注
  • 1309 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號