Obtain CPU usage (Python calls TOP command implementation)

Source: Internet
Author: User

This article is intended to use python to call the TOP command to obtain CPU usage, but there is no idea at the moment.
To obtain the CPU utilization, you can use the top command to redirect it easily. The command is as follows:

 
Top-Bi> cpuhistory. Log
Or
Top-bi | tee cpuhistory. Log

This will not be explained. If you are not familiar with it, please query the top help document. What we need to achieve here is to call the TOP command through Python and obtain the CPU utilization information.
Friends who have used popen will soon be able to think of something similar to the following:Code(This is the first code I wrote ,*_*):

  1  # ! /Usr/bin/python   
2
3 Import OS, time
4
5 time2sleep = 1.5
6 while true:
7 Print OS. popen ( ' top-bi-N 1 ' ). read (). split ( ' \ n ') [2]
8 time. sleep (time2sleep)

The principle looks correct, but there is a problem when running: the idle value of the CPU remains unchanged !!!
The reason is that the command "Top-bi-N 1" is executed separately. You will find that the CPU idle value in the output result remains unchanged.
So you cannot write this ......
Run the "top-bi-N 2" command on the terminal. You will find that the second value is changing every time. This is what we want to achieve.
Considering the time issue, it would be better to write the command like this: "Top-bi-N 2-D 0.02"
The Code is as follows:

 1   #  ! /Usr/bin/Python  
2 '''
3 File: cpurate. py
4 Author: Mike
5 E-mail: Mike_Zhang@live.com
6 '''
7 Import OS, time
8
9 Time2sleep = 2.5
10 While True:
11 Print INT (Time. Time ()),
12 Print OS. popen (' Top-bi-N 2-D 0.02 ' ). Read (). Split ( ' \ N ' ) [1]. Split ( ' \ N ' ) [2]
13 Time. Sleep (time2sleep)

The execution result is as follows:

$./Cpurate. py
1328109437 CPU (s): 10.0% us, 20.0% Sy, 0.0% Ni, 70.0% ID, 0.0% wa, 0.0% hi, 0.0% Si, 0.0% St
1328109441 CPU (s): 0.0% us, 16.7% Sy, 0.0% Ni, 83.3% ID, 0.0% wa, 0.0% hi, 0.0% Si, 0.0% St
1328109444 CPU (s): 0.0% us, 16.7% Sy, 0.0% Ni, 83.3% ID, 0.0% wa, 0.0% hi, 0.0% Si, 0.0% St
1328109447 CPU (s): 12.5% us, 12.5% Sy, 0.0% Ni, 75.0% ID, 0.0% wa, 0.0% hi, 0.0% Si, 0.0% St

Okay, that's all. I hope it will help you.

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.