我通过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被杀死而不是其子级),但我没有这么做。
答案 0 :(得分:0)
我当前的解决方法是
cpulimit.kill() # stop the execution
del cpulimit # terminate the process
dd.send_signal(psutil.signal.SIGCONT) # restore full CPU usage
尽管如此,仍不清楚为什么psutil
的杀死与终端的杀死不同。