实时查看GPU温度

import pynvml
import time


colors = {
    'green': '\033[92m',
    'yellow': '\033[93m',
    'red': '\033[91m',
    'blue': '\033[94m',
    'b_red': '\033[31m',
    'reset': '\033[0m'
}


def change(old_t, new_t):
    if old_t == 0:
        return "————"

    wc = new_t - old_t
    if wc > 0:
        return f"{colors['b_red']}+{wc}{colors['reset']}"
    elif wc < 0:
        return f"{colors['blue']}{wc}{colors['reset']}"
    else:
        return "0"


def get_color(t):
    if t <= 60:
        return colors['green']
    elif t < 80:
        return colors['yellow']
    else:
        return colors['red']


def gpu_temp():
    pynvml.nvmlInit()
    handle = pynvml.nvmlDeviceGetHandleByIndex(0)
    old = 0

    try:
        while True:
            new = pynvml.nvmlDeviceGetTemperature(handle, pynvml.NVML_TEMPERATURE_GPU)
            temp_color = get_color(new)
            ftime = time.strftime("%H:%M:%S", time.localtime())

            print(
                f"{ftime} {temp_color}温度:{new}摄氏度{colors['reset']}\t温度差:{change(old, new)}")

            old = new
            time.sleep(1)
    finally:
        pynvml.nvmlShutdown()


if __name__ == "__main__":
    gpu_temp()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值