"" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "", "" "". "" "" "" "" "" "" "" "," "" "" "," "" "" "," "" "," "" ">> CPU Utilization .py>> Author: Liu Yang>> e-mail: [email protected] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "." "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" #!/usr/bin/env python#-*-coding:utf-8-*-import psutilfrom time Import Sleepdef cpu_utilization (): Cpu_start_alltime=psutil.cpu_times () "Print (Cpu_start_alltime)" What You get: scputimes (user=14088.359375, system=20540.59375, idle=362071.6875, interrupt=847.9375019073486, dpc=379 5.6875) "T1all=sum (Cpu_start_alltime) #开始的cpu时间总和 sleep (1) #休眠一秒 cpu_end_alltime=psutil.cpu_times () t2all=sum ( Cpu_end_alltime) #结束时的cpu时间总和 t1busy=t1all-cpu_start_alltime.idle# start busy time = total time-idle time T2busy=t2all-cpu_end_ Alltime.idle ' CPU Usage: (end of busy time-start busy time)/(total time to end-total time to start) ' cpu_busy= (t2busy-t1busy)/(T2all-t1all) *100 # print ("C PU usage:%0.2f "%cpu_busy+ '% ') return cpu_busyif __name__ = = ' __mAin__ ': Cpu_used=cpu_utilization () print ("CPU Usage:%0.2f"%cpu_used+ '% ') #将得到的使用率一两位小数格式显示
[Python Study Notes] Compute CPU usage