【问题标题】:Monitor CPU temperature in python on windows11在 windows11 上的 python 中监控 CPU 温度
【发布时间】:2022-11-05 03:25:11
【问题描述】:

我编写了一个代码来监视 ram 内存使用情况、CPU 内存使用情况和 CPU 温度我同时使用了 psutil 和 WMI,但我遇到了某种问题,当我有 windows 10 时代码运行完美我更新到 windows 11 它不是在职的。我已经检查了它在 3.10 上的 python 解释器

我得到了我的输出/错误消息:

C:\Users\jeries\PycharmProjects\PP1\venv\Scripts\python.exe C:/Users/jeries/PycharmProjects/PP1/study.py
The CPU usage is:  47.1
RAM memory % used: 54.0
Traceback (most recent call last):
  File "C:\Users\jeries\PycharmProjects\PP1\venv\lib\site-packages\wmi.py", line 880, in query
    return self._namespace.query(wql, self, fields)
  File "C:\Users\jeries\PycharmProjects\PP1\venv\lib\site-packages\wmi.py", line 1072, in query
    return [ _wmi_object(obj, instance_of, fields) for obj in self._raw_query(wql) ]
  File "C:\Users\jeries\PycharmProjects\PP1\venv\lib\site-packages\wmi.py", line 1072, in <listcomp>
    return [ _wmi_object(obj, instance_of, fields) for obj in self._raw_query(wql) ]
  File "C:\Users\jeries\PycharmProjects\PP1\venv\lib\site-packages\win32com\client\dynamic.py", line 324, in __getitem__
    return self._get_good_object_(self._enum_.__getitem__(index))
  File "C:\Users\jeries\PycharmProjects\PP1\venv\lib\site-packages\win32com\client\util.py", line 41, in __getitem__
    return self.__GetIndex(index)
  File "C:\Users\jeries\PycharmProjects\PP1\venv\lib\site-packages\win32com\client\util.py", line 62, in __GetIndex
    result = self._oleobj_.Next(1)
pywintypes.com_error: (-2147217372, 'OLE error 0x80041024', None, None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\jeries\PycharmProjects\PP1\study.py", line 30, in <module>
    temperature_infos = w.Sensor()
  File "C:\Users\jeries\PycharmProjects\PP1\venv\lib\site-packages\wmi.py", line 882, in query
    handle_com_error()
  File "C:\Users\jeries\PycharmProjects\PP1\venv\lib\site-packages\wmi.py", line 258, in handle_com_error
    raise klass(com_error=err)
wmi.x_wmi: <x_wmi: Unexpected COM Error (-2147217372, 'OLE error 0x80041024', None, None)>

Process finished with exit code 1
 

我试过这个:

w = wmi.WMI(namespace="root\openHardwareMonitor")
temperature_infos = w.Sensor()
for sensor in temperature_infos:
    if sensor.SensorType == u'Temperature':
        print(sensor.Name)
        print(sensor.Value)

不工作它说 w.Senosor() “找不到文档”

这是我当前的代码:

import os
import psutil
import wmi


def avg(value_list):
    num = 0
    length = len(value_list)
    for val in value_list:
        num += val
    return num / length


# Calling psutil.cpu_precent() after 2 seconds
print('The CPU usage is: ', psutil.cpu_percent(2))

print('RAM memory % used:', psutil.virtual_memory()[2])
# have the open hardware monitor opened
w = wmi.WMI(namespace="root\\OpenHardwareMonitor")
sensors = w.Sensor()
cpu_temps = []
gpu_temp = 0
for sensor in sensors:
    if sensor.SensorType == u'Temperature' and not 'GPU' in sensor.Name:
        cpu_temps += [float(sensor.Value)]
    elif sensor.SensorType == u'Temperature' and 'GPU' in sensor.Name:
        gpu_temp = sensor.Value



print("Avg CPU: {}".format(avg(cpu_temps)))
print("GPU: {}".format(gpu_temp))

【问题讨论】:

  • 我相信this answer 展示了如何从该错误中获取更多信息。

标签: python python-3.x operating-system wmi psutil


【解决方案1】:

谷歌翻译:

你好,

我为同样的问题搜索了很长时间......终于找到了解决方案:-)

OpenHardwareMonitor 可以生成日志(options/log Sensors)

该日志称为 OpenHardwareMonitorLog-YYYY-MM-DD.csv

因此,想法是预先启动 OpenHardwareMonitor(可能通过计划任务 + 子进程执行,或在启动时自动执行),并在文件的最后一行检索正确的列:


原来的:

你好,

J'ai lontemps cherché pour ce meme problème... et enfin trouvé une solution :-)

OpenHardwareMonitor peut générer des logs(选项/日志传感器)

Le log porte le nom OpenHardwareMonitorLog-YYYY-MM-DD.csv

L'idée est donc de lancer OpenHardwareMonitor au préalable (possible d'executer via tache planifiee + subprocess, ou alors en execution automatique au demarrage), et de récupérer la bonne colne dans la dernière ligne du fichier:


#Code
from datetime import date
while 1 == 1:

    #Génère le nom du log
    now = date.today()
    infile = r"C:OpenHardwareMonitorOpenHardwareMonitorLog-" + now.strftime("%Y-%m-%d") + ".csv"
    
    #Ouvre en lecture seule
    with open(infile, "r") as f:

        f = f.readlines()[-1]    #Lis la dernière ligne
        output = f.split(',')    # Sépare via les ","
        print(output[10])        # 10 = Colonne T°CPU Core #1

【讨论】:

    猜你喜欢
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    • 2020-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多