添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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

python
cpu
temperature
jamieb
jamieb
发布于 2010-03-14
12 个回答
Craig McQueen
Craig McQueen
发布于 2021-06-13
已采纳
0 人赞同

有一个 较新的 "sysfs热区 "API (另见 LWN文章 Linux kernel doc )显示的温度,例如,在

/sys/class/thermal/thermal_zone0/temp

读数的单位是千分之一摄氏度(尽管在较早的内核中,它可能只是摄氏度)。

Giampaolo Rodolà
Giampaolo Rodolà
发布于 2021-06-13
0 人赞同

I recently implemented this in psutil for Linux only.

>>> 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)]}
    
Alex Martelli
Alex Martelli
发布于 2021-06-13
0 人赞同

如果你的Linux支持ACPI,读取伪文件 /proc/acpi/thermal_zone/THM0/temperature (路径可能不同,我知道在一些系统中是 /proc/acpi/thermal_zone/THRM/temperature )应该可以做到。 但我不认为有一种方法可以在 every 世界上的Linux系统,所以你必须更具体地说明你到底有什么Linux!-)

Giampaolo Rodolà
Giampaolo Rodolà
发布于 2021-06-13
0 人赞同

读取文件 /sys/class/hwmon/hwmon*/temp1_* 对我来说是有效的,但是AFAIK没有标准可以做到这一点。 无论如何,你可以试试这个,并确保它提供的CPU数量与" "所示的相同。 sensors " cmdline工具,在这种情况下,你可以认为它是可靠的。

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
    
DrDee
DrDee
发布于 2021-06-13
0 人赞同

Py-cputemp seems to do the job.

py-cputemp基本上是/proc/acpi/thermal_zone上面的一个薄薄的贴面。 这对我来说最初并不奏效,直到我意识到我需要在我的BIOS中启用ACPI。 我禁用了它,因为我认为我不希望在服务器上进行电源管理。 谢谢你的回答;我接受这个答案,因为它是第一个发布的,并引导我思考我的问题的来源。
GLHF
这不是一个答案。
这个库没有维护,甚至没有在PyPI中注册......
另外,这里没有关于如何使用它的说明。
我更喜欢gpiozero from gpiozero import CPUTemperature, LoadAverage cpu = CPUTemperature() print ("CPU温度是%s" % cpu.temperature) load = LoadAverage() print ("CPU温度是%s" % load.load_average)
Pedro
Pedro
发布于 2021-06-13
0 人赞同

Look after pyspectator in pip

Requires python3

from pyspectator import Cpu
from time import sleep
cpu = Cpu(monitoring_latency=1)
while True:
    print (cpu.temperature)
    sleep(1)
    
Lukr
我尝试了你提供的代码,但首先,导入似乎是从pyspectator.processor导入Cpu,如果是Python3,正如警告所说,打印命令应该使用括号......反正这对我来说不起作用,cpu.load显示正确,但温度总是无。
16.04 LTS seems not to like this import.
Greg Hewgill
Greg Hewgill
发布于 2021-06-13
0 人赞同

根据你的Linux发行版,你可能会在 /proc 下找到一个文件,包含这些信息。比如说。 本页 suggests /proc/acpi/thermal_zone/THM/temperature .

yassi
yassi
发布于 2021-06-13
0 人赞同

作为一种选择,你可以安装lm-sensors软件包,然后安装 PySensors (libsensors的一个python绑定)。

不幸的是,PySensors只在Python 2.x版本下运行。
Wolph
Wolph
发布于 2021-06-13
0 人赞同

You could try the PyI2C 模块,它可以直接从内核中读取。

Calvin K
Calvin K
发布于 2021-06-13
0 人赞同

Sysmon很好用。它做得很好,不仅仅是测量CPU温度。它是一个命令行程序,并将其测量的所有数据记录到一个文件中。而且,它是开源的,用python 2.7编写。

Sysmon: https://github.com/calthecoder/sysmon-1.0.1

stefansson
stefansson
发布于 2021-06-13
0 人赞同

I would reflect on 阳光之家(SDsolar) 在上面的解决方法中,对代码进行了一些修改,现在显示的不仅仅是一个值。在while循环之前,你会不断得到CPU温度的实际值

On linux systems:

安装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)
    
Luce
Luce
发布于 2021-06-13
0 人赞同

For Linux systems(Tried on Ubuntu 18.04)

安装 acpi module by 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)