无法通过psutil杀死cpulimit

时间:2018-12-11 15:24:50

标签: python python-3.x kill psutil

我通过Python的psutil执行一个过程,该过程负责繁重的工作。因此,我通过cpulimit限制了它的CPU使用率。

import psutil
dd = psutil.Popen(["dd", "if=/dev/urandom", "of=/dev/zero"])
cpulimit = psutil.Popen(["cpulimit", "-q", "-z", "-p", str(dd.pid), "-l", "10"])

此代码到目前为止有效。但是,我无法杀死cpulimit。在cpulimit.kill()之后,我仍然可以在任务管理器中看到进程ID为cpulimit.pid的进程。首先在del cpulimit时退出该过程。

此外,与通过终端的cpulimit相比,cpulimit.kill()del cpulimit不会恢复dd的全部CPU使用率。

我知道在shell=True中使用psutil.Popen时的杀戮问题(shell被杀死而不是其子级),但我没有这么做。

1 个答案:

答案 0 :(得分:0)

我当前的解决方法是

cpulimit.kill()                          # stop the execution
del cpulimit                             # terminate the process
dd.send_signal(psutil.signal.SIGCONT)    # restore full CPU usage

尽管如此,仍不清楚为什么psutil的杀死与终端的杀死不同。