Message from discussion
subprocess leaves child living
Path: g2news1.google.com!news1.google.com!postnews.google.com!p47g2000hsd.googlegroups.com!not-for-mail
From: Rob Wolfe <r...@smsnet.pl>
Newsgroups: comp.lang.python
Subject: Re: subprocess leaves child living
Date: Tue, 05 Jun 2007 07:06:15 -0700
Organization: http://groups.google.com
Lines: 37
Message-ID: <1181052375.551083.117390@p47g2000hsd.googlegroups.com>
References: <pan.2007.06.05.11.59.09@gmail.com>
<mailman.8717.1181045270.32031.python-list@python.org>
<pan.2007.06.05.13.16.05@gmail.com>
NNTP-Posting-Host: 195.245.68.8
Mime-Version: 1.0
Content-Type: text/plain; charset="us-ascii"
X-Trace: posting.google.com 1181052375 9433 127.0.0.1 (5 Jun 2007 14:06:15 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Tue, 5 Jun 2007 14:06:15 +0000 (UTC)
In-Reply-To: <pan.2007.06.05.13.16.05@gmail.com>
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4,gzip(gfe),gzip(gfe)
Complaints-To: groups-abuse@google.com
Injection-Info: p47g2000hsd.googlegroups.com; posting-host=195.245.68.8;
posting-account=KJyoUA0AAADNqgFFA-fZh22XmxH10GJy
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.
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