subprocess leaves child living

Thomas Dybdahl Ahle lobais at gmail.com
Wed Jun 6 08:11:01 EDT 2007


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.



More information about the Python-list mailing list