美文网首页
psutil 模块的安装和使用

psutil 模块的安装和使用

作者: Manchangdx | 来源:发表于2018-08-31 15:03 被阅读0次

    哈哈~

    一、简介

    psutil 是一个跨平台库
    能够轻松实现获取系统运行的进程和系统利用率
    包括 CPU、内存、磁盘、网络等信息

    psutil 主要应用于系统监控、分析和限制系统资源及进程的管理

    psutil 实现了同等命令行工具提供的功能,如 ps、top、lsof、netstat、ifconfig、who、df、kill、free、nice、ionice、iostat、iotop、uptime、pidof、tty、taskset、pmap 等。目前支持 32 位和 64 位的 Linux、Windows、OS X、FreeBSD 和 Sun Solaris 等操作系统

    安装方法:

    sudo pip3 install psutil
    

    二、基本用法

    获取 CPU 信息:

    In [3]: import psutil
    
    In [4]: psutil.cpu_count()   # 逻辑 cpu 的个数
    Out[4]: 2
    
    In [5]: psutil.cpu_count(logical=False)   # 物理 cpu 的个数
    Out[5]: 2
    

    获取内存信息:

    In [6]: mem = psutil.virtual_memory()
    
    In [7]: mem   # 全部信息
    Out[7]: svmem(total=4112044032, available=1084837888, 
    percent=73.6, used=2760781824, free=700198912, 
    active=1513553920, inactive=797716480, buffers=71434240, 
    cached=579629056, shared=33579008, slab=121634816)
    
    In [8]: mem.total   # 总内存
    Out[8]: 4112044032
    
    In [9]: mem.available   # 可用内存
    Out[9]: 1084837888
    
    In [10]: mem.used   # 已用内存
    Out[10]: 2760781824
    

    获取 swap 内存信息:

    In [11]: psutil.swap_memory()
    Out[11]: sswap(total=4291817472, used=507510784, 
    free=3784306688, percent=11.8, sin=22065152, sout=518934528)
    

    获取进程信息:

    In [18]: psutil.pids()   # 全部 PID
    Out[18]: 
    [1,
     2,
     4,
     6,
    ... ...
     19383,
     19402,
     19542]
    
    In [19]: p = psutil.Process(19542)   # 某个进程的信息
    
    In [20]: p
    Out[20]: psutil.Process(pid=19542, name='kworker/u256:2', started='16:44:28')
    
    In [21]: p.status
    Out[21]: <bound method Process.status of 
    psutil.Process(pid=19542, name='kworker/u256:2', started='16:44:28')>
    
    In [22]: p.status()
    Out[22]: 'idle'
    

    获取当前网络连接信息:

    # 需要用 sudo 运行,否则会报错:AccessDenied
    # 因为 psutil 获取信息也要通过系统接口,而获取网络连接需要 root 权限
     ~  sudo ipython
    Password:
    Python 3.7.0 (default, Jul 23 2018, 20:22:55)
    Type 'copyright', 'credits' or 'license' for more information
    IPython 6.5.0 -- An enhanced Interactive Python. Type '?' for help.
    
    In [1]: import psutil
    
    In [2]: psutil.net_connections()   # 获取网络连接信息
    Out[2]:
    [sconn(fd=4, family=<AddressFamily.AF_INET: 2>, type=2, 
    laddr=addr(ip='0.0.0.0', port=0), raddr=(), status='NONE', pid=4627),
     sconn(fd=8, family=<AddressFamily.AF_INET: 2>, type=2, 
    laddr=addr(ip='0.0.0.0', port=0), raddr=(), status='NONE', pid=4627),
    ... ...
    

    类似 Linux 的 ps 命令:

    In [8]: psutil.test()
    USER         PID %MEM     VSZ     RSS TTY           START    TIME  COMMAND
    root           0 25.5 74629472 2139112 ?             Aug31   33:05  kernel_task
    root           1  0.1 4328532    9236 ?             Aug31   01:32  launchd
    root          39    ? 4306056    1152 ?             Aug31   00:03  syslogd
    root          40  0.2 4337260   15900 ?             Aug31   00:09  UserEventAgent
    

    相关文章

      网友评论

          本文标题:psutil 模块的安装和使用

          本文链接:https://www.haomeiwen.com/subject/uegswftx.html