[Python Study Notes] Calculates CPU usage v0.1

Source: Internet
Author: User
Tags clear screen cpu usage

V0.1 Update log:

1. Join the platform to determine, support Windows and Linux

2. Automatic clear screen display, more intuitive display


"" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "", "" "". "" "" "" "" "" "" "" "," "" "" "," "" "" "," "" "," "" ">> CPU Utilization .py>> Author: Liu Yang>> e-mail: [email protected] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "." "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "#!/usr/bin/env python#-*-coding:utf-8-*-import os,sysimport Psutilfrom Time Import Sleepdef cpu_utilization (): Cpu_start_alltime=psutil.cpu_times () "Print (Cpu_start_alltime) Content: Scputimes (user=14088.359375, system=20540.59375, idle=362071.6875, interrupt=847.93750190     73486, dpc=3795.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 (" CPU usage:%0.2f "%cpu_busy+ '% ') return cpu_busyif __name__ = = ' __main__ ': While True:cpu_used=cpu_utilization () # Print (sys.platform) #判断当前的平台 platform        =sys.platform if platform = = "Linux": Os.system ("clear") Else:os.system (' CLS ') Print ("CPU Usage:%0.2f"%cpu_used+ '% ') #将得到的使用率一两位小数格式显示

[Python Study Notes] Compute CPU Usage v0.1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.