[英]How can I get a continuous reading of CPU temperature for Linux based operating systems using Python?
我正在尝试为树莓派制作程序,但我一直在Mac和树莓派之间进行切换。 有没有一种方法可以使用Python通过Linux访问CPU Temp。 我可以使其与tkinter
吗? (据我所知tkinter
无法正确使用while
循环
# How can I get a continuous CPU temperature reading for Linux (Mac//Raspberry Pi) using a
def function():
#stuff
tkinterFrame.after(delay, function)
# im using tkinter so avoiding while loops
# need to use function .after()```
I expect a continuous output that refreshes every second.
当您读取文件/sys/class/thermal/thermal_zone0/temp
时,Linux会为您提供CPU的当前温度。 您将获得一行文本,温度为Integer 。 因此,您必须将结果除以1000
才能获得以°C
为单位的温度。 请看一下这个简单的示例,该示例读取当前时间和温度以在终端上进行打印。
import time
import datetime
while(True):
CurrentTime = datetime.datetime.now()
with open(r"/sys/class/thermal/thermal_zone0/temp") as File:
CurrentTemp = File.readline()
print(str(CurrentTime) + " - " + str(float(CurrentTemp) / 1000))
time.sleep(1)
您现在要做的就是存储结果并打印出来(也许有图吗?)。 您可以使用一个额外的线程来执行此操作,因此当您使用某种延迟时,您的应用程序不会卡住(因为您不需要每毫秒读取一次温度-我认为每一秒钟就足够了)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.