python3.6.8 查看机器负载

import psutil
import time,traceback
from IPython.display import clear_output

def get_disk_mbps(interval=1):
    disk_io_prev = psutil.disk_io_counters()
    time.sleep(interval)
    disk_io_now = psutil.disk_io_counters()
    bytes_read = disk_io_now.read_bytes - disk_io_prev.read_bytes
    bytes_written = disk_io_now.write_bytes - disk_io_prev.write_bytes
    mbps_read = bytes_read / interval / 1024 / 1024
    mbps_written = bytes_written / interval / 1024 / 1024
    mbps = mbps_read + mbps_written
    return int(mbps)

def get_cpu_usage():
    cpu_percent = psutil.cpu_percent()
    return int(cpu_percent)

def get_memory_usage():
    return psutil.virtual_memory().percent

count = 0
while True:
    mbps   = get_disk_mbps(1)
    cpu    = get_cpu_usage()
    memory = get_memory_usage()
    print(f"{mbps} MB/s,cpu {cpu},memory {memory}")
    
    time.sleep(2)
    if(count % 10 == 0):
        clear_output()
    count += 1

你可能感兴趣的:(python,开发语言)