簡體   English   中英

如何使用Python連續讀取基於Linux的操作系統的CPU溫度?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM