如何杀死由python中的子进程创建的进程



在Linux Ubuntu操作系统下,我运行包含GObject循环的test.py scrip,使用subprocess

subprocess.call(["test.py"])

现在,这个test.py将创建过程。有没有办法在Python中杀死这个过程?注意:我不知道进程 ID。

如果我没有非常清楚地解释我的问题,我很抱歉,因为我是这种形式的新手,并且一般对 python 很陌生。

我建议不要使用subprocess.call,而是构造一个Popen对象并使用其API:http://docs.python.org/2/library/subprocess.html#popen-objects

特别:http://docs.python.org/2/library/subprocess.html#subprocess.Popen.terminate

哼!

subprocess.call()只是subprocess.Popen().wait()

from subprocess import Popen
from threading import Timer
p = Popen(["command", "arg1"])
print(p.pid) # you can save pid to a file to use it outside Python
# do something else..
# now ask the command to exit
p.terminate()
terminator = Timer(5, p.kill) # give it 5 seconds to exit; then kill it
terminator.start()
p.wait()
terminator.cancel() # the child process exited, cancel the hit

subprocess.call等待进程完成并返回退出代码(整数(值,因此无法知道子进程的进程ID。你应该考虑使用subprocess.Popen哪个forks((子进程。

最新更新