Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > subprocess leaves child living

Reply
Thread Tools

subprocess leaves child living

 
 
Thomas Dybdahl Ahle
Guest
Posts: n/a
 
      06-05-2007
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"
 
Reply With Quote
 
 
 
 
Stefan Sonnenberg-Carstens
Guest
Posts: n/a
 
      06-05-2007
Thomas Dybdahl Ahle schrieb:
> 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"
>

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.
 
Reply With Quote
 
 
 
 
Thomas Dybdahl Ahle
Guest
Posts: n/a
 
      06-05-2007
Den Tue, 05 Jun 2007 14:07:44 +0200 skrev Stefan Sonnenberg-Carstens:

> Thomas Dybdahl Ahle schrieb:
>> 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"
>>

> 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.


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.
 
Reply With Quote
 
Rob Wolfe
Guest
Posts: n/a
 
      06-05-2007

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

 
Reply With Quote
 
Thomas Dybdahl Ahle
Guest
Posts: n/a
 
      06-05-2007
Den Tue, 05 Jun 2007 07:06:15 -0700 skrev Rob Wolfe:

> 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>


But you can't ever catch sigkill.
Isn't there a way to make sure the os kills the childprocess when the
parrent dies?
 
Reply With Quote
 
Rob Wolfe
Guest
Posts: n/a
 
      06-05-2007
Thomas Dybdahl Ahle <> writes:

> But you can't ever catch sigkill.


There is no protection against sigkill.

> Isn't there a way to make sure the os kills the childprocess when the
> parrent dies?


If the parent dies suddenly without any notification childprocesses
become zombies and there is no way to avoid that.

--
HTH,
Rob
 
Reply With Quote
 
Michael Bentley
Guest
Posts: n/a
 
      06-05-2007

On Jun 5, 2007, at 3:01 PM, Rob Wolfe wrote:

> Thomas Dybdahl Ahle <> writes:
>
>> But you can't ever catch sigkill.

>
> There is no protection against sigkill.
>
>> Isn't there a way to make sure the os kills the childprocess when the
>> parrent dies?

>
> If the parent dies suddenly without any notification childprocesses
> become zombies and there is no way to avoid that.


Apologies for picking nits...

But actually *that* is an orphan process. When a parent process dies
and the child continues to run, the child becomes an orphan and is
adopted by init. Orphan processes can be cleaned up on most Unices
with 'init q' (or something very similar).

Zombies on the other hand, are those processes that have completed
execution but still have an entry in the process table. The cause of
zombies AFAIK, is a parent that has failed to call wait(2). To clean
up zombies, you can send a SIGCHLD signal to the parent process --
probably with 'kill -17' (but use 'kill -l' to find out what it is on
your system).

hth,
Michael
---
"I would rather use Java than Perl. And I'd rather be eaten by a
crocodile than use Java." — Trouser


 
Reply With Quote
 
Thomas Dybdahl Ahle
Guest
Posts: n/a
 
      06-05-2007
Den Tue, 05 Jun 2007 22:01:44 +0200 skrev Rob Wolfe:

> Thomas Dybdahl Ahle <> writes:
>
>> But you can't ever catch sigkill.

>
> There is no protection against sigkill.
>
>> Isn't there a way to make sure the os kills the childprocess when the
>> parrent dies?

>
> If the parent dies suddenly without any notification childprocesses
> become zombies and there is no way to avoid that.


If I call "kill -9 pythonpid" on the python code you posted earlier, the
terminal running the script will continue pinging forever.
If it was a harder program like a chessengine or such, it would continue
consuming 100% cpu time.
Zombies would be fine to me.
 
Reply With Quote
 
Thomas Dybdahl Ahle
Guest
Posts: n/a
 
      06-05-2007
Den Tue, 05 Jun 2007 15:46:39 -0500 skrev Michael Bentley:

> But actually *that* is an orphan process. When a parent process dies
> and the child continues to run, the child becomes an orphan and is
> adopted by init. Orphan processes can be cleaned up on most Unices with
> 'init q' (or something very similar).


Is it not possible to tell python that this process should not be adopted
by init, but die with its parrent?
Just like terminals seem to do it..
 
Reply With Quote
 
Michael Bentley
Guest
Posts: n/a
 
      06-05-2007

On Jun 5, 2007, at 4:17 PM, Thomas Dybdahl Ahle wrote:

> Den Tue, 05 Jun 2007 15:46:39 -0500 skrev Michael Bentley:
>
>> But actually *that* is an orphan process. When a parent process dies
>> and the child continues to run, the child becomes an orphan and is
>> adopted by init. Orphan processes can be cleaned up on most
>> Unices with
>> 'init q' (or something very similar).

>
> Is it not possible to tell python that this process should not be
> adopted
> by init, but die with its parrent?
> Just like terminals seem to do it..


Well, the way you posed the original question:

> from subprocess import Popen
> popen = Popen(["ping", "google.com"])
> from time import sleep
> sleep(100)


is really what adoption by init is designed to handle. Here you've
created a child but have not waited for its return value. Like a
good adoptive parent, init will wait(2) for the child.

I think if you really looked into it you'd find that the terminal had
called wait(2) before it was killed. Similarly, if you start a long-
running subprocess in python and wait for it to return -- killing the
parent will slaughter the child as well.

hth,
Michael
---
Let the wookie win.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to import subprocess into my 'subprocess.py' file hiral Python 2 05-05-2010 12:56 PM
Newbie help for using multiprocessing and subprocess packages forcreating child processes Rob Newman Python 0 06-16-2009 07:13 PM
subprocess -popen - reading stdout from child - hangs gregpinero@gmail.com Python 7 09-25-2007 02:42 AM
[Subprocess/Windows] subprocess module under Windows 98 Andreas Jung Python 2 11-02-2005 05:41 PM
How do I: Main thread spawn child threads, which child processes...control those child processes? Jeff Rodriguez C Programming 23 12-09-2003 11:06 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57