Skip to content

Python获取Windows CPU和内存占用

2021-04-12

pip3 install psutil
import psutil
import time


def getMemory():
    data = psutil.virtual_memory()
    memory = str(int(round(data.percent))) + "%"
    return memory

def getCpu():
    cpu = str(round(psutil.cpu_percent(interval=1), 2)) + "%"
    return cpu


def main():
    while (True):
        memory = getMemory()
        cpu = getCpu()
        time.sleep(0.2)
        print("CPU占用:%s \t 内存占用:%s"%(cpu, memory))


if __name__ == "__main__":
    main()

内存没啥毛病,CPU的好像不太准(比实际小一点点),等着我再看看有没有啥更好的方法更新在这里

Ukraine 在俄罗斯对乌克兰发动的野蛮的侵略战争中矢志不渝地支持乌克兰