如何在Python中获取当前的CPU和RAM使用率?


如何在Python中获取当前的CPU和RAM使用率?


使用psutil库

获取一些内存和CPU统计信息:

from __future__ import print_function
import psutil
print(psutil.cpu_percent())
print(psutil.virtual_memory()) #  physical memory usage

或者这样:

import os
import psutil
pid = os.getpid()
py = psutil.Process(pid)
memoryUse = py.memory_info()[0]/2.**30  # memory use in GB...I think
print('memory use:', memoryUse)