首页 > 解决方案 > 无法通过 psutil 杀死 cpulimit

问题描述

psutil我通过负责重负载的 Python 执行一个进程。因此,我通过cpulimit.

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.

我知道使用shell=Truein时的杀戮问题psutil.Popen(外壳被杀死而不是它的孩子),但我不这样做。

标签: pythonpython-3.xkillpsutil

解决方案


我目前的解决方法是

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

然而,目前还不清楚为什么psutil' 的杀戮与终端的杀戮不同。


推荐阅读