如何使用Python检索CPU的温度? (假设我在Linux上)
答案 0 :(得分:15)
有一个newer "sysfs thermal zone" API(另见LWN article和Linux kernel doc)显示温度低于例如
/sys/class/thermal/thermal_zone0/temp
读数是千分之一摄氏度(虽然在较旧的内核中,它可能只是C级)。
答案 1 :(得分:8)
如果你的Linux支持ACPI,那么读取伪文件/proc/acpi/thermal_zone/THM0/temperature
(路径可能不同,我知道在某些系统中它是/proc/acpi/thermal_zone/THRM/temperature
)应该这样做。但我认为世界上的每个 Linux系统都没有办法,所以你必须更具体地了解你所拥有的Linux! - )
答案 2 :(得分:7)
Py-cputemp似乎可以胜任。
答案 3 :(得分:6)
读取 / sys / class / hwmon / hwmon * / temp1 _ * 中的文件对我有用,但是AFAIK没有完全干净的标准。 无论如何,您可以尝试这一点,并确保它提供“传感器”cmdline实用程序显示的相同数量的CPU,在这种情况下,您可以认为它是可靠的。
from __future__ import division
import os
from collections import namedtuple
_nt_cpu_temp = namedtuple('cputemp', 'name temp max critical')
def get_cpu_temp(fahrenheit=False):
"""Return temperatures expressed in Celsius for each physical CPU
installed on the system as a list of namedtuples as in:
>>> get_cpu_temp()
[cputemp(name='atk0110', temp=32.0, max=60.0, critical=95.0)]
"""
# http://www.mjmwired.net/kernel/Documentation/hwmon/sysfs-interface
cat = lambda file: open(file, 'r').read().strip()
base = '/sys/class/hwmon/'
ls = sorted(os.listdir(base))
assert ls, "%r is empty" % base
ret = []
for hwmon in ls:
hwmon = os.path.join(base, hwmon)
label = cat(os.path.join(hwmon, 'temp1_label'))
assert 'cpu temp' in label.lower(), label
name = cat(os.path.join(hwmon, 'name'))
temp = int(cat(os.path.join(hwmon, 'temp1_input'))) / 1000
max_ = int(cat(os.path.join(hwmon, 'temp1_max'))) / 1000
crit = int(cat(os.path.join(hwmon, 'temp1_crit'))) / 1000
digits = (temp, max_, crit)
if fahrenheit:
digits = [(x * 1.8) + 32 for x in digits]
ret.append(_nt_cpu_temp(name, *digits))
return ret
答案 4 :(得分:5)
我最近在psutil中仅针对Linux实现了此功能。
>>> import psutil
>>> psutil.sensors_temperatures()
{'acpitz': [shwtemp(label='', current=47.0, high=103.0, critical=103.0)],
'asus': [shwtemp(label='', current=47.0, high=None, critical=None)],
'coretemp': [shwtemp(label='Physical id 0', current=52.0, high=100.0, critical=100.0),
shwtemp(label='Core 0', current=45.0, high=100.0, critical=100.0),
shwtemp(label='Core 1', current=52.0, high=100.0, critical=100.0),
shwtemp(label='Core 2', current=45.0, high=100.0, critical=100.0),
shwtemp(label='Core 3', current=47.0, high=100.0, critical=100.0)]}
答案 5 :(得分:3)
在pyspectator
pip
需要python3
from pyspectator import Cpu
from time import sleep
cpu = Cpu(monitoring_latency=1)
while True:
print (cpu.temperature)
sleep(1)
答案 6 :(得分:2)
根据您的Linux发行版,您可能会在/proc
下找到包含此信息的文件。例如,this page建议/proc/acpi/thermal_zone/THM/temperature
。
答案 7 :(得分:1)
作为替代方案,您可以安装lm-sensors软件包,然后安装PySensors(libsensors的python绑定)。
答案 8 :(得分:0)
您可以尝试PyI2C模块,它可以直接从内核中读取。
答案 9 :(得分:0)
Sysmon很好用。制作精良,它不仅可以测量CPU温度。它是一个命令行程序,并将测量的所有数据记录到文件中。此外,它是开源的,用python 2.7编写。
答案 10 :(得分:0)
我会反思 SDsolar 上面的解决方案,稍微修改了代码。现在它不仅显示了一个值。直到 while 循环,您才能不断获得 CPU 温度的实际值
在 Linux 系统上:
安装pyspectator模块:
pip install pyspectator
将此代码放入文件'cpu-temp.py'
#!/usr/bin/env python3
from pyspectator.processor import Cpu
from time import sleep
while True:
cpu = Cpu(monitoring_latency=1) #changed here
print (cpu.temperature)
sleep(1)
答案 11 :(得分:-1)
对于 Linux 系统(在 Ubuntu 18.04 上试过)
通过以下方式安装 acpi 模块
sudo apt install acpi
运行 acpi -V
应该会为您提供大量有关系统的信息。现在我们只需要通过python获取温度值即可。
import os
os.system("acpi -V > output.txt")
battery = open("output.txt", "r")
info = battery.readline()
val = info.split()
percent4real = val[3]
percentage = int(percent4real[:-1])
print(percentage)
percentage
变量将为您提供温度。
因此,首先我们在文本文件中获取 acpi -V
命令的输出,然后读取它。因为数据都是String类型的,所以需要转换成整数。