子过程离开了孩子的生活 [英] subprocess leaves child living
问题描述
当我从子进程导入Popen
popen = Popen([" ping,google。 com"])
从时间导入睡眠
睡眠(100)
启动并杀死它,ping进程生活on。
有没有办法确保在
python进程的时候ping进程总是被杀死?
我不能使用atexit,因为ping不会在python被杀死时被杀死
艰难的方式
Hi, When I do a small program like
from subprocess import Popen
popen = Popen(["ping", "google.com"])
from time import sleep
sleep(100)
start it and kill it, the ping process lives on.
Is there a way to ensure that the ping process is always killed when the
python process is?
I can''t use atexit, as ping then isn''t killed when python is killed "in
the hard way"
推荐答案
Thomas Dybdahl Ahle schrieb:
Thomas Dybdahl Ahle schrieb:
当我从子流程导入Popen
这样的小程序时>
popen = Popen([ping,google.com])
从进口时间进入睡眠状态
sleep(100)>
启动并杀死它,ping进程继续运行。
有没有办法确保ping进程总是被杀死当
python进程是?
我不能使用atexit,因为当python被杀死时ping不会被杀死
困难的方式
Hi, When I do a small program like
from subprocess import Popen
popen = Popen(["ping", "google.com"])
from time import sleep
sleep(100)
start it and kill it, the ping process lives on.
Is there a way to ensure that the ping process is always killed when the
python process is?
I can''t use atexit, as ping then isn''t killed when python is killed "in
the hard way"
或许打电话给popen.close()?
你基本上打开一个管道,产生一个外壳,那么命令是
在那里开始。
所以,如果你的程序退出,产生的shell仍然存在,只有
管道是死了。
Calling popen.close() perhaps ?
You basically open a pipe, which spawns a shell and the command is then
started in there.
So, if your program quits, the spawned shell is still alive, only the
pipe is dead.
Den Tue,2007年6月5日14:07:44 +0200 skrev Stefan Sonnenberg-Carstens:
Den Tue, 05 Jun 2007 14:07:44 +0200 skrev Stefan Sonnenberg-Carstens:
Thomas Dybdahl Ahle schrieb:
Thomas Dybdahl Ahle schrieb:
>当我从子流程导入Popen做一个像
这样的小程序时
popen = Popen([ping,google.com])从时间导入睡眠
睡眠(100)
启动它并杀死它,ping过程继续存在。有没有办法确保在python进程
时总是杀死ping进程?
我不能使用atexit,因为当python是ping时ping不会被杀死杀死了在艰难的路上
>Hi, When I do a small program like
from subprocess import Popen
popen = Popen(["ping", "google.com"]) from time import sleep
sleep(100)
start it and kill it, the ping process lives on. Is there a way to
ensure that the ping process is always killed when the python process
is?
I can''t use atexit, as ping then isn''t killed when python is killed "in
the hard way"
或许打电话给popen.close()?
你基本上打开一个烟斗,它产生一个shell然后命令是
在那里开始。
所以,如果你的程序退出,那么生成的shell仍然存在,只有
管道已经死了。
Calling popen.close() perhaps ?
You basically open a pipe, which spawns a shell and the command is then
started in there.
So, if your program quits, the spawned shell is still alive, only the
pipe is dead.
问题是 - 当我被杀时我不能这样做。
是不是可以打开这样的流程终端的方式?如果我敲了b $ b就杀了终端,那里打开的东西也会死掉。
Problem is - I can''t do that when I get killed.
Isn''t it possible to open processes in such a way like terminals? If I
kill the terminal, everything open in it will die too.
Thomas Dybdahl Ahle写道:
Thomas Dybdahl Ahle wrote:
问题是 - 当我被杀的时候我不能这样做。
是不是可以打开这样的流程终端的方式?如果我要杀掉终端,那么打开它的一切都会死掉。
Problem is - I can''t do that when I get killed.
Isn''t it possible to open processes in such a way like terminals? If I
kill the terminal, everything open in it will die too.
在POSIX平台上你可以使用信号和``os.kill``函数。
例如:
< code>
导入操作系统,信号
来自子进程导入Popen
来自导入时间的睡眠
def处理程序(signum,frame):
print''信号处理程序调用''
提高KeyboardInterrupt
signal.signal(signal.SIGTERM,handler)
尝试:
popen = Popen([" ping",google。 com"])
试试:
sleep(100)
除了KeyboardInterrupt:
pass
最后:
如果popen.poll()为None:
print" kill process:%d" %popen.pid
os.kill(popen.pid,signal.SIGTERM)
< / code>
- -
HTH,
Rob
On POSIX platform you can use signals and ``os.kill`` function.
Fo example:
<code>
import os, signal
from subprocess import Popen
from time import sleep
def handler(signum, frame):
print ''Signal handler called''
raise KeyboardInterrupt
signal.signal(signal.SIGTERM, handler)
try:
popen = Popen(["ping", "google.com"])
try:
sleep(100)
except KeyboardInterrupt:
pass
finally:
if popen.poll() is None:
print "killing process: %d" % popen.pid
os.kill(popen.pid, signal.SIGTERM)
</code>
--
HTH,
Rob
这篇关于子过程离开了孩子的生活的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!