尽量避免我的电脑在运行对 CPU 要求较高的程序时需要关闭。我做错了什么?

问题描述 投票:0回答:1

我的电脑有一个自动保护系统,可以防止过热,因此当它达到一定温度时,它会通过关闭来保护自己。我喜欢这个,因为我对我的计算机进行了大量投资。然而,我想在这些热事件发生之前找出答案。能够保存我的工作。在第一阶段,我试图获取温度,然后我的计划是将声音警报设置为“黄色”。

但是,我还没有获得我的CPU温度。我做错了什么?

以下是我当前的代码:


import cpuinfo
from colorama import Fore, Back, Style, init

# Inicializar colorama
init(autoreset=True)

# Función para obtener la temperatura de la CPU
def get_cpu_temperature():
    try:
        info = cpuinfo.get_cpu_info()
        temperature = info.get('temperature', None)
        return temperature
    except Exception as e:
        return f"{Fore.CYAN}Error al obtener temperatura: {e}"

# Función para mostrar la temperatura con colores
def display_temperature(temperature):
    if temperature is None:
        print(f"{Fore.CYAN}No se pudo obtener la temperatura.")
    else:
        temp_str = f"Temperatura: {temperature}°C"
        if temperature < 40:
            print(f"{Fore.GREEN}{temp_str}")
        elif temperature < 70:
            print(f"{Fore.YELLOW}{temp_str}")
        else:
            print(f"{Fore.RED}¡ALERTA! {temp_str}")

# Obtener la temperatura de la CPU
temperature = get_cpu_temperature()

# Mostrar la temperatura o el mensaje de error con colores
display_temperature(temperature)

python cpu temperature
1个回答
0
投票

当我运行你的代码时,我得到:

没有温度。

最可能的原因是因为温度不是可用的项目之一,假设您像我一样使用

py-cpuinfo
。可用项目显示在此处(请参阅“字段”标题下)。

© www.soinside.com 2019 - 2024. All rights reserved.