subprocess leaves child living

Michael Bentley michael at jedimindworks.com
Wed Jun 6 08:44:03 EDT 2007


On Jun 6, 2007, at 7:11 AM, Thomas Dybdahl Ahle wrote:

> Den Tue, 05 Jun 2007 17:41:47 -0500 skrev Michael Bentley:
>
>> On Jun 5, 2007, at 5:13 PM, Michael Bentley wrote:
>>
>>
>>> 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.
>>
>> I guess I should have verified my suspicions before speaking up --  
>> I was
>> worng -- even if you are waiting for a return code, the child will
>> persist as a child of init.
>
> Bugger.
> I know that if you use forkpty, the child get slaughtered, but  
> using the
> subprocess module just seems much easier than forking.

Yeah, and if there is some way (there may be, but I don't know) to  
make your python script the head of a process group (or session --  
I'm not very clear on the distinction) -- children would be  
automatically slain.  That's how a terminal session manages to get  
its children cleaned up I think...

If it is important that children get terminated upon death of a  
parent, you probably do have to fork.

g'luck!
Michael
---
"Neurons are far more valuable than disk space, screen lines, or CPU  
cycles." - Ben Finney







More information about the Python-list mailing list