美文网首页
2020-11-25 python好库推荐之psutil

2020-11-25 python好库推荐之psutil

作者: 树上的鱼zZ | 来源:发表于2020-11-25 20:12 被阅读0次

psutil是一个跨平台库(http://pythonhosted.org/psutil/)能够轻松实现获取系统运行的进程和系统利用率(包括CPU、内存、磁盘、网络等)信息。它主要用来做系统监控,性能分析,进程管理。它实现了同等命令行工具提供的功能,如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等操作系统.

Kill process tree

```

import os

import signal

import psutil

def kill_proc_tree(pid, sig=signal.SIGTERM, include_parent=True,

                   timeout=None, on_terminate=None):

    """Kill a process tree (including grandchildren) with signal

    "sig" and return a (gone, still_alive) tuple.

    "on_terminate", if specified, is a callabck function which is

    called as soon as a child terminates.

    """

    assert pid !=

 os.getpid(), "won't kill myself"

    parent = psutil.Process(pid)

    children = parent.children(recursive=True)

    if include_parent:

        children.append(parent)

    for p in children:

        p.send_signal(sig)

    gone, alive = psutil.wait_procs(children, timeout=timeout,

                                    callback=on_terminate)

    return (gone, alive)

```

相关文章

网友评论

      本文标题:2020-11-25 python好库推荐之psutil

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