杀死使用Python的subprocess.Popen()创建的进程 [英] Killing a process created with Python's subprocess.Popen()
问题描述
这是我的想法:
首先,我使用subprocess.Popen创建了一个进程
First of all, I created a process by using subprocess.Popen
第二,经过一定时间后,我尝试通过Popen.kill()杀死它
Second, after certain amount of time, I tried to kill it by Popen.kill()
import subprocess
import os, signal
import time
proc1 = subprocess.Popen("kvm -hda /path/xp.img", shell = True)
time.sleep(2.0)
print 'proc1 = ', proc1.pid
subprocess.Popen.kill(proc1)
但是,"proc1"在Popen.kill()之后仍然存在. 有专家可以告诉我如何解决这个问题吗? 感谢您的考虑.
However, "proc1" still exists after Popen.kill(). Could any experts tell me how to solve this issue? I appreciate your considerations.
感谢所有专家的意见,我做了您所建议的一切,但结果仍然保持不变.
Thanks to the comments from all experts, I did all you recommended, but result still remains the same.
proc1.kill() #it sill cannot kill the proc1
os.kill(proc1.pid, signal.SIGKILL) # either cannot kill the proc1
谢谢你们.
我仍在等待您解决这一微妙问题的宝贵经验.
And I am still waiting for your precious experience on solving this delicate issue.
推荐答案
在您的代码中应该是
proc1.kill()
kill
或terminate
都是Popen
对象的方法,该对象将信号signal.SIGKILL
发送到进程.
Both kill
or terminate
are methods of the Popen
object which sends the signal signal.SIGKILL
to the process.
这篇关于杀死使用Python的subprocess.Popen()创建的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!