我正在嘗試制作一個腳本,它可以讀取 CPU 溫度并在它開始變得太熱時自動關閉行程。我使用過 psutil,但我不知道如何從那里獲取價值。這就是 psutil.sensors_temperatures() 輸出和一堆 RuntimeWarning: ignoring FileNotFoundError 一起給出的。
{'acpitz': [shwtemp(label='', current=60.0, high=103.0, critical=103.0)], 'pch_cannonlake': [shwtemp(label='', current=53.0, high=None, critical=None)], 'coretemp': [shwtemp(label='Package id 0', current=60.0, high=100.0, critical=100.0), shwtemp(label='Core 0', current=59.0, high=100.0, critical=100.0), shwtemp(label='Core 1', current=59.0, high=100.0, critical=100.0), shwtemp(label='Core 2', current=60.0, high=100.0, critical=100.0), shwtemp(label='Core 3', current=57.0, high=100.0, critical=100.0)], 'iwlwifi_1': [shwtemp(label='', current=49.0, high=None, critical=None)]}
如何訪問“coretemp”中的當前值?那不是顯示CPU溫度的主要內容嗎?或者它是“acpitz”?
uj5u.com熱心網友回復:
您有一個包含psutil._common.shwtemp物件的串列字典。
快速查看psutil源代碼表明這些實際上是namedtuple:
# psutil/_common.py
shwtemp = namedtuple(
'shwtemp', ['label', 'current', 'high', 'critical'])
所以,你可以簡單地做:
# core number 0, current temperature
d['coretemp'][0].current
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/352679.html
