如何每 15 秒获得 cpu 统计数据? python

how to get cpu stats every 15 seconds ? python

看我在用

    import psutil
    d =  psutil.cpu_percent(1.5)
    e =  psutil.cpu_percent(1.5)
    f  =  psutil.cpu_percent(1.5)
    g  =  psutil.cpu_percent(1.5)
    
    val = (d + e +f+g)/4
    print(d , e , f, g)
    print(val)

当我使用 interval 时,我的 import psutil 无法访问

只需使用 time 模块,使用间隔为 15 秒的 while 循环。

所以,它应该是这样的:

try:
    import psutil, time
    while True:
        d = psutil.cpu_percent(1.5)
        e = psutil.cpu_percent(1.5)
        f = psutil.cpu_percent(1.5)
        g = psutil.cpu_percent(1.5)
        
        val = (d + e + f + g)/4
        print(d, e, f, g)
        print(val)

        time.sleep(15)
except Exception as e:
    print(str(e).capitalize())
except KeyboardInterrupt:
    print("Finished!")