Python prints a list of the programs that occupy memory Top10 in tabular form

Source: Internet
Author: User

This topic is related to Python's operation of the process, the number of cycles for a loop count, sorting and printing the table, and so on, as follows:

650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;border-bottom:0px;border-left:0px; " Border= "0" alt= "image" Src= "Http://s3.51cto.com/wyfs02/M01/87/E0/wKiom1fjlJHQo9qxAABFEPuFGyw567.png" height= "414" />

650) this.width=650; "title=" image "style=" border-top:0px;border-right:0px;border-bottom:0px;border-left:0px; " Border= "0" alt= "image" Src= "Http://s3.51cto.com/wyfs02/M00/87/DC/wKioL1fjlJKj0p2OAAAyTL0z1zc540.png" height= "303" />

The code is as follows:

#!/usr/bin/python# encoding: utf-8# -*- coding: utf8 -*-"" "Created by  pycharm.file:                linuxbashshellscriptforops:performanceops.pyuser:                guodongcreate date:        2016/9/ 21create time:        18:11  "" "Import psutilimport  prettytableps_result = list () For proc in psutil.process_iter ():     ps_result.append ({' Name ':  proc.name (),  ' pid ': proc.pid,  ' Cpu_percent ':  Proc.cpu_percent (),                        ' memory_percent ':  proc.memory_percent ()}) table =  Prettytable. Prettytable () Table.field_names = ["No.",  "Name",  "pid",  "Memory percent"]for i, item in  enumerate (Sorted (ps_result, key=lambda x: x[' memory_percent '], reverse=true)):     table.add_row ([i + 1, item[' name '], item[' pid '], format (item[') Memory_percent '] / 100,  '. 2% ')])     if i >= 9:         breakprint table

The code can also be obtained from GitHub, https://github.com/DingGuodong/LinuxBashShellScriptForOps/blob/master/functions/process/performanceOps.py

It uses two major third-party modules, Psutil (for getting process information) and prettytable (for printing tables), available on both Windows and Linux systems, if prompted with "Importerror:no module named xxxx", You can execute the command pip install xxxx or easy_install xxxx.

--end--

This article is from "Communication, My Favorites" blog, please make sure to keep this source http://dgd2010.blog.51cto.com/1539422/1855467

Python prints a list of the programs that occupy memory Top10 in tabular form

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.