파이썬을 사용하여 CPU 온도를 얻습니까? [닫은
-
19-09-2019 - |
문제
Python을 사용하여 CPU의 온도를 검색하려면 어떻게합니까? (내가 Linux에 있다고 가정)
해결책
py-cputemp 일을하는 것 같습니다.
다른 팁
이있다 최신 "SYSFS 열 영역"API (또한보십시오 LWN 기사 그리고 Linux 커널 문서) 예를 들어 온도를 보여줍니다
/sys/class/thermal/thermal_zone0/temp
판독 값은 천분의 1도 셀치우스에 있습니다 (오래된 커널에서는 비록 c 도일 수 있습니다).
Linux가 ACPI를 지원하는 경우 의사 파일을 읽습니다 /proc/acpi/thermal_zone/THM0/temperature
(길은 다를 수 있습니다. /proc/acpi/thermal_zone/THRM/temperature
일부 시스템에서는) 그렇게해야합니다. 하지만 나는 작동하는 방법이 있다고 생각하지 않습니다. 모든 세계의 Linux 시스템이므로 Linux가 정확히 무엇인지보다 구체적으로해야합니다!-)
파일을 읽습니다 /sys/class/hwmon/hwmon*/temp1_* 나를 위해 일했지만 afaik이를 깨끗하게하는 표준은 없습니다. 어쨌든, 당신은 이것을 시도하고 그것이 표시된 동일한 수의 CPU를 제공 할 수 있는지 확인할 수 있습니다.센서"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
나는 최근에 이것을 구현했다 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)]}
돌봐 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에 대한 파이썬 바인딩).
당신은 시도 할 수 있습니다 pyi2c 모듈은 커널에서 직접 읽을 수 있습니다.
Sysmon은 잘 작동합니다. 멋지게 만들어지면 CPU 온도를 측정하는 것 이상의 기능을 수행합니다. 명령 줄 프로그램이며 파일로 측정 한 모든 데이터를 기록합니다. 또한 오픈 소스이며 Python 2.7로 작성되었습니다.