如何使用 Python 检索 CPU 的温度?(假设我在 Linux 上)
12 回答
有一个较新的“sysfs 热区”API(另请参见LWN 文章和Linux 内核文档)显示温度,例如
/sys/class/thermal/thermal_zone0/temp
读数以千分之一摄氏度为单位(尽管在较旧的内核中,它可能只是摄氏度)。
我最近在psutil for 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)]}
如果你的 Linux 支持 ACPI,读取伪文件/proc/acpi/thermal_zone/THM0/temperature
(路径可能不同,我知道它/proc/acpi/thermal_zone/THRM/temperature
在某些系统中)应该这样做。但我不认为有一种方法适用于世界上的每个Linux 系统,所以你必须更具体地了解你所拥有的 Linux!-)
读取/sys/class/hwmon/hwmon*/temp1_*中的文件对我有用,但 AFAIK 没有标准可以干净地做到这一点。无论如何,您可以尝试这个并确保它提供与“传感器”命令行实用程序显示的相同数量的 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
Py-cputemp似乎可以完成这项工作。
照顾pyspectator
进来pip
需要python3
from pyspectator import Cpu
from time import sleep
cpu = Cpu(monitoring_latency=1)
while True:
print (cpu.temperature)
sleep(1)
根据您的 Linux 发行版,您可能会在下面找到/proc
包含此信息的文件。例如,此页面建议/proc/acpi/thermal_zone/THM/temperature
.
作为替代方案,您可以安装 lm-sensors 包,然后安装PySensors(用于 libsensors 的 python 绑定)。
你可以试试PyI2C模块,它可以直接从内核读取。
Sysmon 很好用。制作精良,它不仅可以测量 CPU 温度。它是一个命令行程序,并将它测量的所有数据记录到一个文件中。此外,它是开源的,用 python 2.7 编写。
我会反思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)
适用于 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 类型。
- 注意:此命令在 WSL 中使用时不显示 CPU 温度