python实时得到cpu和内存的使用情况

时间:2022-05-20 22:31:38
#先下载psutil库:pip install psutil
import psutil
import os,datetime,time

def getMemCpu():
        data = psutil.virtual_memory()
        total = data.total #总内存,单位为byte
        free = data.available #可以内存
        memory =  "Memory usage:%d"%(int(round(data.percent)))+"%"+" "
        cpu = "CPU:%0.2f"%psutil.cpu_percent(interval=1)+"%"
        return memory+cpu

def main():

        while(True):
            info = getMemCpu()
            time.sleep(0.2)
            print info+"\b"*(len(info)+1),

if      __name__=="__main__":
        main()

python实时得到cpu和内存的使用情况