Learning Python(15)--第三方psutil模块

本文详细介绍了psutil库的功能,包括获取系统利用率、CPU信息、内存使用状况、硬盘划分及使用率、网络I/O设备使用状况等,并提供了相关API的使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

psutil是一个可以第三方跨平台Python库,它可以获取正在运行的进程的状态信息,还可以得到系统的利用率(包括CPU,内存,硬盘,网络等).一般用于系统的监控,分析和限制进程资源进程的管理

psutil主页:https://pypi.python.org/pypi/psutil

CPU

  • CPU利用率
    psutil.cpu_percent(interval=None, percpu=False)

Warning:the first time this function is called with interval = 0.0 or None it will return a meaningless 0.0 value which you are supposed to ignore.

  • CPU个数
    psutil.cpu_count(logical=True)logical为True,返回逻辑CPU的个数,logical为False,返回物理CPU的个数.

Memory

  • 内存使用状况
    psutil.virtual_memory(),返回一个nametuple’,有以下字段
    • total: total physical memory available.
    • available: the actual amount of available memory that can be given instantly to processes that request more memory in bytes; this is calculated by summing different memory values depending on the platform (e.g. free + buffers + cached on Linux) and it is supposed to be used to monitor actual memory usage in a cross platform fashion.
    • percent: the percentage usage calculated as (total - available) / total * 100.
    • used: memory used, calculated differently depending on the platform and designed for informational purposes only.
    • free: memory not being used at all (zeroed) that is readily available; note that this doesn’t reflect the actual memory available (use ‘available’ instead).

Disk

  • 硬盘划分状况
    psutil.disk_partitions(all=False)将所以挂载的硬盘信息作为一个nametuple返回.

  • 硬盘使用率
    psutil.disk_usage(path)
    例:

>>> import psutil
>>> psutil.disk_usage('/')
sdiskusage(total=46132547584L, used=35164180480L, free=8601366528L, percent=76.2)

Network

  • 网络I/O设备使用状况
    psutil.net_io_counters(pernic=False)

  • 当前主机的网络连接状况
    psutil.net_connections(kind='inet')返回一个nametuple,包括以下字段:

    • fd: the socket file descriptor, if retrievable, else -1. If the connection refers to the current process this may be passed to socket.fromfd() to obtain a usable socket object.
    • family: the address family, either AF_INET, AF_INET6 or AF_UNIX.
    • type: the address type, either SOCK_STREAM or SOCK_DGRAM.
    • laddr: the local address as a (ip, port) tuple or a path in case of AF_UNIX sockets.
    • raddr: the remote address as a (ip, port) tuple or an absolute path in case of UNIX sockets. When the remote endpoint is not connected you’ll get an empty tuple (AF_INET*) or None (AF_UNIX). On Linux AF_UNIX sockets will always have this set to None.
    • status: represents the status of a TCP connection. The return value is one of the psutil.CONN_* constants (a string). For UDP and UNIX sockets this is always going to be psutil.CONN_NONE.
    • pid: the PID of the process which opened the socket, if retrievable, else None. On some platforms (e.g. Linux) the availability of this field changes depending on process privileges (root is needed).
  • 主机网络接口地址信息
    psutil.net_if_addrs(),类型,地址等信息

  • 主机网络接口状态信息
    psutil.net_if_stats(),包含速率,MTU的信息

ps,关于网络接口的一些函数,就相当于ifconfig命令.

Other system info

  • boot time:psutil.boot_time()
  • users:psutil.users()

Process class

class psutil.Process(pid=None)

  • pid:进程id
  • ppid():父进程id
  • name():进程名
  • p.memory_percent(memtype=”rss”):进程p占用的内存比
  • p.suspend() —挂起进程
  • p.resume() —恢复进程
  • p.terminate() —Terminate the process with SIGTERM signal
  • p.kill() —Kill the current process by using SIGKILL signal
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值