【记录】折腾Python中的psutil:一个用于获得处理器和系统相关信息的模块

无意间得知了psutil模块,去网上找了下,找到:

http://pypi.python.org/pypi/psutil

目前最新版是0.6.1

打算去折腾折腾。

不过先看看介绍。


psutil简介

psutil是什么

psutil是个Python模块。

psutil能干啥

psutil能干的事是:提供了个接口,可以用来获取信息,包括:

  • 当前运行的进程
  • 系统(资源使用)信息
    • CPU
    • 内存
    • 磁盘
    • 网络
    • 用户

psutil实现了很多功能,包括了如下工具所具有的:

  • ps
  • top
  • df
  • kill
  • free
  • lsof
  • free
  • netstat
  • ifconfig
  • nice
  • ionice
  • iostat
  • iotop
  • uptime
  • pidof
  • tty
  • who
  • taskset
  • pmap

 

由此可见,也就可以利用psutil去实现kill的功能,而把某个进程干掉。

下载psutil

找到下载的地方:

http://code.google.com/p/psutil/downloads/list

下载和我当前系统:

win7 64位

Python 2.7.2

对应的:

psutil-0.6.1.win-amd64-py2.7.exe

得到

psutil-0.6.1.win-amd64-py2.7.exe

安装psutil

双击psutil-0.6.1.win-amd64-py2.7.exe去安装:
install psutil ui

此处可以正确的检测出Python的安装路径:

can detect python27 install dir

准备安装:

ready to install

很快就安装完毕了:

install complete

 

使用psutil

试着去写写代码。

此处,尝试去找到,当前进程中正在运行的有道词典的信息,并打印出来。

进程名是:YodaoDict.exe

然后再干掉此进程。

然后参考:

http://pypi.python.org/pypi/psutil

中的“Process management”,去写代码。

#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Function:
【记录】折腾Python中的psutil:一个用于获得处理器和系统相关信息的模块
http://www.crifan.com/try_python_psutil

Author:     Crifan Li
Version:    2013-01-02
Contact:    admin at crifan dot com
"""

import psutil;

def tryPsutil():
    pidList = psutil.get_pid_list();
    print "pidList=",pidList;
    
    #processToTest = "QQ.exe";
    processToTest = "YodaoDict.exe";
    
    for eachPid in pidList:
        try:
            eachProcess = psutil.Process(eachPid);
            #print "eachProcess=",eachProcess;
            processName = eachProcess.name;
            if(processName.lower() == processToTest.lower()):
                print "Found process";
                print "processName=",processName;
                processExe = eachProcess.exe;
                print "processExe=",processExe;
                processGetcwd = eachProcess.getcwd();
                print "processGetcwd=",processGetcwd;
                processCmdline = eachProcess.cmdline;
                print "processCmdline=",processCmdline;
                processStatus = eachProcess.status;
                print "processStatus=",processStatus;
                processUsername = eachProcess.username;
                print "processUsername=",processUsername;
                processCreateTime = eachProcess.create_time;
                print "processCreateTime=",processCreateTime;
                print "Now will terminate this process !";
                eachProcess.terminate();
                eachProcess.wait(timeout=3);
                print "psutil.test()=",psutil.test();
                
        except psutil.NoSuchProcess,pid:
            print "no process found with pid=%s"%(pid);

if __name__ == "__main__":
    tryPsutil();

完整的输出效果是:

E:\Dev_Root\python\try_psutil>try_psutil.py
pidList= [0, 4, 412, 496, 564, 576, 620, 628, 636, 700, 784, 860, 928, 992, 172, 452, 1064, 1124, 1208, 1412, 1448, 1504
, 1644, 1696, 1760, 1784, 1884, 1960, 2024, 1136, 1340, 2156, 2264, 2296, 2316, 2444, 2584, 2592, 2712, 2756, 2952, 3004
, 2480, 2988, 2108, 3268, 3808, 4008, 2512, 3580, 4284, 5040, 2092, 4120, 5060, 1524, 4436, 5488, 5520, 5888, 5696, 1912
, 1660, 8432, 1060, 976, 7416, 7656, 1736, 5412, 9628, 9712, 10072, 15508, 19368, 21096, 17132, 24396, 26680, 34056, 290
20, 31976, 24472, 32380, 32544, 25272, 35712, 23604, 32516, 36484, 35852, 37540, 37236, 37348, 38820, 37256, 38304, 3807
2, 36392, 22808]
Found process
processName= YodaoDict.exe
processExe= D:\Program Files (x86)\Youdao\Dict4\YodaoDict.exe
processGetcwd= C:\Windows
processCmdline= ['D:\\Program Files (x86)\\Youdao\\Dict4\\YodaoDict.exe', 'start', '-hide']
processStatus= running
processUsername= NYBDHB7EB3XEHP6\Administrator
processCreateTime= 1357032858.0
Now will terminate this process !
psutil.test()=USER         PID %CPU %MEM     VSZ     RSS TTY           START    TIME  COMMAND
SYSTEM         0  0.0    ?       ?      24 ?             Jan01   49:51  System Idle Process
SYSTEM         4  0.0    ?     132     380 ?             Jan01   43:10  System
             172  0.0  0.2   11172   15164 ?             Jan01   00:18  svchost.exe
             412  0.0    ?     560     500 ?             Jan01   00:00  smss.exe
             452  0.0  0.8   47740   51376 ?             Jan01   04:49  svchost.exe
             496  0.0  0.1    2664    3196 ?             Jan01   00:04  csrss.exe
             564  0.0    ?    1724    1404 ?             Jan01   00:00  wininit.exe
             576  0.0  0.6    4012   36836 ?             Jan01   05:42  csrss.exe
             620  0.0  0.1    4964    6744 ?             Jan01   00:05  services.exe
             628  0.0  0.2    7116    9640 ?             Jan01   00:45  lsass.exe
             636  0.0  0.1    3460    3444 ?             Jan01   00:11  lsm.exe
             700  0.0  0.1    3396    4200 ?             Jan01   00:00  winlogon.exe
             784  0.0  0.1    5696    7244 ?             Jan01   02:45  svchost.exe
             860  0.0  0.1    6228    7652 ?             Jan01   00:07  svchost.exe
             928  0.0    ?    1728    2132 ?             Jan01   00:00  atiesrxx.exe
Administra   976  0.0  4.0  178760  247656 ?             Jan01   37:11  360se.exe
             992  0.0  0.2   21888   14048 ?             Jan01   00:58  svchost.exe
Administra  1060  0.0  0.6   98736   33944 ?             Jan01   00:49  QQExternal.exe
            1064  0.0  0.2    7556   10620 ?             Jan01   03:03  svchost.exe
            1124  0.0  0.1    2768    4524 ?             Jan01   00:00  atieclxx.exe
            1136  0.0    ?    1612    1484 ?             Jan01   00:00  IMEDICTUPDATE.EXE
            1208  0.0  0.4   27944   26804 ?             Jan01   06:29  svchost.exe
            1340  0.0  0.2  144172   12580 ?             Jan01   19:20  sqlservr.exe
            1412  0.0  0.1    6792    8664 ?             Jan01   00:03  spoolsv.exe
            1448  0.0  0.3    9676   16436 ?             Jan01   00:37  svchost.exe
            1504  0.0  0.2   10884   12756 ?             Jan01   00:33  svchost.exe
Administra  1524  0.0  0.2   11016   10572 ?             Jan01   00:13  QQProtect.exe
Administra  1644  0.0  0.1    2628    3988 ?             Jan01   00:00  taskeng.exe
Administra  1660  0.0  0.1    4284    3752 ?             Jan01   00:00  rundll32.exe
Administra  1696  0.0  0.9   41660   52284 ?             Jan01   19:30  dwm.exe
Administra  1736  0.0  0.6   45340   38496 ?             Jan01   00:09  360se.exe
Administra  1760  0.0    ?   10964    1188 ?             Jan01   03:44  GooglePinyinDaemon.exe
Administra  1784  0.0  1.6  101440   99904 ?             Jan01   04:51  explorer.exe
            1884  0.0  0.1    6192    8124 ?             Jan01   19:21  AlipaySecSvc.exe
            1912  0.0  0.4   47028   21968 ?             Jan01   05:25  svchost.exe
            1960  0.0  0.2   16520   14060 ?             Jan01   00:21  DTLService.exe
            2024  0.0  0.2   10096   13324 ?             Jan01   00:02  IcbcDaemon_64.exe
Administra  2092  0.0    ?    1212     692 ?             Jan01   00:00  GooglePinyinService.exe
Administra  2108  0.0  0.1    4780    6636 ?             Jan01   00:27  WPService.exe
            2156  0.0  0.1    3940    3652 ?             Jan01   00:00  TenpayServer.exe
            2264  0.0    ?    2196    2596 ?             Jan01   00:00  sqlwriter.exe
Administra  2296  0.0  0.1    8600    5028 ?             Jan01   00:00  RAVCpl64.exe
            2316  0.0    ?    2204    3052 ?             Jan01   00:00  svchost.exe
            2444  0.0  0.1    4252    4900 ?             Jan01   00:01  WLIDSVC.EXE
Administra  2480  0.0  0.2   10356   12348 ?             Jan01   00:15  wandoujia_helper.exe
            2512  0.0  0.3   31548   17804 ?             Jan01   00:47  SearchIndexer.exe
Administra  2584  0.0    ?    1956    2384 ?             Jan01   00:01  ctfmon.exe
Administra  2592  0.0  0.7   33232   39988 ?             Jan01   02:48  picpick.exe
Administra  2712  0.0  0.2    8032    9828 ?             Jan01   00:33  TSVNCache.exe
            2756  0.0    ?    1504    1352 ?             Jan01   00:00  WLIDSVCM.EXE
Administra  2988  0.0    ?    1216    1140 ?             Jan01   00:00  jusched.exe
Administra  3004  0.0  0.1    5252    8444 ?             Jan01   02:52  RenRenService.exe
Administra  3268  0.0  0.1   40984    4996 ?             Jan01   00:02  MOM.exe
            3580  0.0  0.2    6280    9724 ?             Jan01   00:06  OSPPSVC.EXE
Administra  3808  0.0  0.1  112036    6108 ?             Jan01   00:44  CCC.exe
Administra  4008  0.0  0.1    4072    4840 ?             Jan01   00:02  WordBook.exe
Administra  4120  0.0    ?    2616    2616 ?             Jan01   00:06  wandoujia_daemon.exe
            4284  0.0  0.1    2528    3476 ?             Jan01   00:16  svchost.exe
Administra  4436  0.0  3.1  272020  190740 ?             Jan01   23:57  QQ.exe
Administra  5040  0.0  0.2    7404    9336 ?             Jan01   17:43  Alipaybsm.exe
Administra  5060  0.0    ?    2104    2452 ?             Jan01   00:00  conhost.exe
Administra  5412  0.0  1.4   88396   83880 ?             Jan01   00:34  360se.exe
Administra  5488  0.0    ?    1492    1620 ?             Jan01   00:00  TXPlatform.exe
Administra  5520  0.0  0.2   11376   12816 ?             Jan01   00:01  BFVDesktop.exe
            5696  0.0    ?    1392    1280 ?             Jan01   00:00  svchost.exe
            5888  0.0  0.1   27820    7984 ?             Jan01   00:00  PresentationFontCache.exe
Administra  7416  0.0  0.5   38564   28756 ?             Jan01   00:02  360se.exe
Administra  7656  0.0    ?   15656    1936 ?             Jan01   00:23  360seNotify.exe
Administra  8432  0.0  2.8  174100  170100 ?             Jan01   05:17  xntalk.exe
Administra  9628  0.0  0.9   58392   56696 ?             Jan01   44:49  360se.exe
Administra  9712  0.0  0.6   57184   38136 ?             Jan01   00:14  360se.exe
Administra 10072  0.0  0.7   50592   45444 ?             Jan01   00:44  360se.exe
Administra 15508  0.0  1.4   83116   83504 ?             Jan01   00:53  360se.exe
Administra 17132  0.0  0.7   55712   42712 ?             Jan01   00:19  explorer.exe
           19368  0.0    ?    3300    2736 ?             Jan01   00:02  SearchProtocolHost.exe
Administra 21096  0.0  0.5   34800   32656 ?             Jan01   00:12  explorer.exe
Administra 22808  0.0  0.2    7456   11080 ?             17:52   00:06  python.exe
           23604  0.0  0.3   20416   19920 ?             16:46   00:16  audiodg.exe
Administra 24396  0.0  0.1    5452    4640 ?             Jan01   00:02  SogouCloud.exe
Administra 24472  0.0  0.6   26668   37268 ?             02:28   03:34  ThunderPlatform.exe
Administra 25272  0.0  0.5   23196   32928 ?             15:00   05:00  AlipaySafeTran.exe
SYSTEM     26680  0.0    ?    3308    2980 ?             00:17   00:00  SearchFilterHost.exe
Administra 29020  0.0  0.7   39320   45516 ?             02:16   00:07  360se.exe
Administra 31976  0.0  1.6   81512   98160 ?             02:28   09:27  Thunder.exe
Administra 32380  0.0  1.4   75472   85876 ?             14:39   02:10  360se.exe
Administra 32516  0.0  0.5   28212   31000 ?             16:47   00:00  360se.exe
Administra 32544  0.0  0.5   31912   33572 ?             14:55   00:03  360se.exe
Administra 34056  0.0  0.6   60452   37664 ?             02:08   07:40  QQExternal.exe
Administra 35712  0.0  0.8   12776   52096 ?             16:25   00:05  rrbrowser.exe
Administra 35852  0.0  2.7  115688  164640 ?             16:50   01:19  WindowsLiveWriter.exe
Administra 36392  0.0  0.1    2284    6900 ?             17:29   00:01  conhost.exe
Administra 36484  0.0  0.4   20220   23300 ?             16:47   00:05  360se.exe
Administra 37236  0.0  0.6   29856   33916 ?             16:59   00:03  360se.exe
Administra 37256  0.0  0.5   17056   29612 ?             17:18   01:05  notepad++.exe
Administra 37336  0.0  0.3   11344   18764 ?             17:52   00:01  PinyinUp.exe
Administra 37348  0.0  0.5   30344   33576 ?             16:59   00:02  360se.exe
Administra 37540  0.0  0.1    4972    8968 ?             16:54   00:00  rundll32.exe
Administra 38072  0.0  0.1    2920    6796 ?             17:29   00:00  cmd.exe
Administra 38304  0.0  0.2    4008   13268 ?             17:23   01:21  taskmgr.exe
Administra 38820  0.0  0.7   24468   41916 ?             17:16   00:02  explorer.exe
None

E:\Dev_Root\python\try_psutil>
E:\Dev_Root\python\try_psutil>

另外的,关于相关的其函数,比如kill,都可以参考官网的解释:

http://code.google.com/p/psutil/wiki/Documentation

比如:

  • terminate()

    Terminate the process with SIGTERM signal. On Windows this is an alias for kill().

  • kill()

    Kill the current process by using SIGKILL signal.

 

总结

psutil,效果还是很不错的。



3 Thoughts on “【记录】折腾Python中的psutil:一个用于获得处理器和系统相关信息的模块

  1. Pingback: Python psutil模块-IT大道

  2. Pingback: 【记录】折腾Python中的psutil:一个用于获得处理器和系统相关信息的模块_Linux运维工程师的技术博客

  3. 有点小问题,

    psutil.Process(pid=852)
    psutil.Process(pid=880, name=’WINLOGON.EXE’)

    有的进程没有name,结果就报错了.

发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量