繁体   English   中英

使用 Python 获取 CPU 温度?

[英]Getting CPU temperature using Python?

如何使用 Python 检索 CPU 的温度? (假设我在 Linux 上)

有一个较新的“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 没有干净地执行此操作的标准。 无论如何,您可以尝试此操作并确保它提供与“ sensors ” cmdline 实用程序显示的 CPU 数量相同的 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似乎可以完成这项工作。

pip照顾pyspectator

需要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 编写。

系统门: https : //github.com/calthecoder/sysmon-1.0.1

我会反思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 上试过)

通过sudo apt install acpi安装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 温度

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM