如何使用Python获取CPU温度?

30 投票
12 回答
66953 浏览
提问于 2025-04-15 20:24

我怎么用Python获取我的CPU温度?(假设我在使用Linux系统)

12 个回答

20

我最近在Linux上实现了这个功能,使用的是psutil这个库。

>>> 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)]}
23

有一个更新的“sysfs热区”API,详细信息可以在这个链接找到(你也可以看看这篇LWN文章Linux内核文档)。这个API可以显示温度,比如说:

/sys/class/thermal/thermal_zone0/temp

读取的温度单位是摄氏度的千分之一(不过在旧版本的内核中,可能只是以摄氏度为单位)。

6

Py-cputemp 这个工具看起来可以完成这个任务。

撰写回答