有的时候需要对python程序内存占用进行监控,这个时候可以用到psutil库,Anaconda中是自带的,如果import出错,可以用pip install psutil(安装在python中)或conda install psutil(安装在Anaconda中)
#常用的:
#python2
import psutil
import os
info = psutil.virtual_memory()
print u'内存使用:',psutil.Process(os.getpid()).memory_info().rss
print u'总内存:',info.total
print u'内存占比:',info.percent
print u'cpu个数:',psutil.cpu_count()
#常用的:
#python3
import psutil
import os
info = psutil.virtual_memory()
print(u'内存使用:',psutil.Process(os.getpid()).memory_info().rss)
print(u'总内存:',info.total)
print(u'内存占比:',info.percent)
print(u'cpu个数:',psutil.cpu_count())
其他内置的方法或属性还有:
boot_time
callable
collections
cpu_count
cpu_percent
cpu_stats
cpu_times
cpu_times_percent
disk_io_counters
disk_partitions
disk_usage
errno
functools
long
net_connections
net_if_addrs
net_if_stats
net_io_counters
os
pid_exists
pids
process_iter
pwd
signal
subprocess
swap_memory
sys
test
time
traceback
users
version_info
virtual_memory
wait_procs
win_service_get
win_service_iter
查看windows开机时间
import time
import psutil
print (u'电脑开机时间:{}'.format(time.strftime('%y-%m-%d %H:%M:%S', time.localtime(psutil.boot_time()))))