使用 Python Windows 获取 CPU 和 GPU Temp [英] Get CPU and GPU Temp using Python Windows
问题描述
我想知道是否有办法在 python 中获取 CPU 和 GPU 温度.我已经找到了适用于 Linux 的方法(使用
注意:如果要获取温度,需要以管理员身份运行.如果没有,你只会得到Load
的值.
我对中文博客
做了一些改动I was wondering if there was a way to get the CPU and the GPU temperature in python. I have already found a way for Linux (using psutil.sensors_temperature()
), and I wanted to find a way for Windows.
A way to find the temperatures for Mac OS would also be appreciated, but I mainly want a way for windows.
I prefer to only use python modules downloadable with pip, but I am fine with downloading extra things. If your answer requires extra files, I would prefer for it to not require too many files.
When I try doing the below, I get None:
import wmi
w = wmi.WMI()
prin(w.Win32_TemperatureProbe()[0].CurrentReading)
When I try doing the below, I get an error:
import wmi
w = wmi.WMI(namespace="root\wmi")
temperature_info = w.MSAcpi_ThermalZoneTemperature()[0]
print(temperature_info.CurrentTemperature)
Error:
wmi.x_wmi: <x_wmi: Unexpected COM Error (-2147217396, 'OLE error 0x8004100c', None, None)>
I have heard of OpenHardwareMoniter, but this requires me to install something that is not a python module. I would also prefer to not have to run the script as admin to get the results.
I am also fine with running windows cmd commands with python, but I have not found one that returns the CPU temp.
Update: I found this: https://stackoverflow.com/a/58924992/13710015.
I can't figure out how to use it though.
When I tried doing: print(OUTPUT_temp._fields_)
, I got
[('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'>)]
Note: I really do not want to run this as admin. If I absolutely have to, I can, but I prefer not to.
Edit: I am also fine with using C/C++ extensions with Python, or any way to the CPU temp in python.
I think there doesn't have a directly way to achieve that.Some CPU producers wouldn't provide wmi
to let your know the temperature directly.
You could use OpenHardwareMoniter.dll
.Use the dynamic library.
Firstly, Download the OpenHardwareMoniter
.It contains a file called OpenHardwareMoniter.dll
.
Install the module pythonnet
:
pip install pythonnet
Below code works fine on my PC(Get the CPU temperature):
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()
To Get the GPU temperature,change the c.Hardware[0]
to c.Hardware[1]
.
Compare the result with :
Attention: If you want to get the temperature, you need to run it as Administrator. If not, you will only get the value of Load
.
I did some changes from a Chinese Blog
这篇关于使用 Python Windows 获取 CPU 和 GPU Temp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!