web-dev-qa-db-ja.com

Pythonを使用してCPU温度を取得していますか?

Pythonを使用してCPUの温度を取得するにはどうすればよいですか? (私がLinuxを使っていると仮定します)

26
jamieb

Py-cputemp がその仕事をしているようです。

7
DrDee

新しい「sysfs Thermal Zone」API があります(- LWN記事 および Linuxカーネルdoc も参照)。

/sys/class/thermal/thermal_zone0/temp

読み値は摂氏1000分の1単位です(古いカーネルでは、摂氏は摂氏であった可能性があります)。

15
Craig McQueen

LinuxがACPIをサポートしている場合は、疑似ファイル/proc/acpi/thermal_zone/THM0/temperature(パスは異なる場合があります。/proc/acpi/thermal_zone/THRM/temperature一部のシステムでは)行う必要があります。しかし、私はevery Linuxシステムで機能する方法は世界にないと思います。そのため、使用しているLinuxを正確に特定する必要があります!-)

8
Alex Martelli

/ sys/class/hwmon/hwmon */temp1 _ *でファイルを読み取ることは私にとってはうまくいきましたが、私の知る限り、これをきれいに行うための標準はありません。とにかく、これを試して、 "sensors" 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
6

最近、これを 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

pyspectatorpipの世話をする

Python3が必要

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

while True:
    print (cpu.temperature)
    sleep(1)
3
Pedro

Linuxディストリビューションによっては、/procの下にこの情報を含むファイルが見つかる場合があります。たとえば、 このページ/proc/acpi/thermal_zone/THM/temperatureを提案します。

2
Greg Hewgill

別の方法として、lm-sensorsパッケージをインストールし、次に PySensors (a python libsensorsのバインディング))をインストールできます。

1
yassi

PyI2C モジュールを試すことができます。カーネルから直接読み取ることができます。

0
Wolph

Sysmonはうまく動作します。うまくできていて、CPU温度を測定するだけではありません。これはコマンドラインプログラムであり、測定したすべてのデータをファイルに記録します。また、それはオープンソースであり、python 2.7。

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

0
calvin k