Obtener la temperatura de la CPU usando Python? [cerrado]


¿Cómo puedo recuperar la temperatura de mi CPU usando Python? (Suponiendo que estoy en Linux)

Author: SilentGhost, 2010-03-14

10 answers

Py-cputemp parece hacer el trabajo.

 8
Author: DrDee,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-25 09:24:00

Hay una API más nueva de "sysfs thermal zone" (ver también artículo LWN y Linux kernel doc ) que muestra las temperaturas bajo, por ejemplo,

/sys/class/thermal/thermal_zone0/temp

Las lecturas están en milésimas de grados Celsius (aunque en núcleos más antiguos, puede haber sido solo grados C).

 13
Author: Craig McQueen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-22 22:32:00

Si su Linux soporta ACPI, leyendo pseudo-archivo /proc/acpi/thermal_zone/THM0/temperature (la ruta puede diferir, sé que es /proc/acpi/thermal_zone/THRM/temperature en algunos sistemas) debería hacerlo. Pero no creo que haya una manera que funcione en todos los sistemas Linux en el mundo, por lo que tendrá que ser más específico sobre exactamente qué Linux tiene!-)

 8
Author: Alex Martelli,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-03-13 23:42:04

Leer archivos en /sys/class/hwmon/hwmon*/temp1_* funcionó para mí, pero AFAIK no hay estándares para hacer esto limpiamente. De todos modos, puede probar esto y asegurarse de que proporciona el mismo número de CPU que muestra la utilidad cmdline "sensors", en cuyo caso puede asumir que es confiable.

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
 5
Author: Giampaolo Rodolà,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-04-27 20:03:44

Cuidar pyspectator en pip

Requiere python3

from pyspectator import Cpu
from time import sleep
cpu = Cpu(monitoring_latency=1)

while True:
    print (cpu.temperature)
    sleep(1)
 3
Author: Pedro,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-22 17:20:40

Dependiendo de su distribución de Linux, puede encontrar un archivo bajo /proc que contenga esta información. Por ejemplo, esta página sugiere /proc/acpi/thermal_zone/THM/temperature.

 2
Author: Greg Hewgill,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-03-13 23:41:20

Recientemente implementé esto en psutil solo para 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)]}
 2
Author: Giampaolo Rodolà,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-06 13:19:01

Como alternativa puede instalar el paquete lm-sensors, luego instalar PySensors (un enlace de python para libsensors).

 1
Author: yassi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-25 09:24:17

Puede probar el módulo PyI2C, que puede leer directamente desde el núcleo.

 0
Author: Wolph,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-03-13 23:41:52

Sysmon funciona bien. Muy bien hecho, hace mucho más que medir la temperatura de la CPU. Es un programa de línea de comandos, y registra todos los datos que midió en un archivo. Además, es de código abierto y está escrito en python 2.7.

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

 0
Author: calthecoder,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-26 17:28:44