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