Best way to prevent zombie processes

Cecil Westerhof Cecil at decebal.nl
Mon Jun 1 05:20:17 EDT 2015


Op Monday 1 Jun 2015 00:22 CEST schreef Marko Rauhamaa:

> Cecil Westerhof <Cecil at decebal.nl>:
>
>> At the moment I have the following code:
>> os.chdir(directory)
>> for document in documents:
>> subprocess.Popen(['evince', document])
>>
>> With this I can open several documents at once. But there is no way
>> to know when those documents are going to be closed. This
>> could/will lead to zombie processes. (I run it on Linux.) What is
>> the best solution to circumvent this?
>>
>> I was thinking about putting all Popen instances in a list. And
>> then every five minutes walk through the list and check with poll
>> if the process has terminated. If it has it can be released from
>> the list. Of-course I need to synchronise those events. Is that a
>> good way to do it?
>
> If you don't care to know when child processes exit, you can simply
> ignore the SIGCHLD signal:
>
> import signal
> signal.signal(signal.SIGCHLD, signal.SIG_IGN)
>
> That will prevent zombies from appearing.

In this case I do not care. I just do not want to create zombie
processes.

It works. What I find kind of strange because
    https://docs.python.org/2/library/signal.html
says:
    signal.SIG_DFL

        This is one of two standard signal handling options; it will
        simply perform the default function for the signal. For
        example, on most systems the default action for SIGQUIT is to
        dump core and exit, while the default action for SIGCHLD is to
        simply ignore it.

    signal.SIG_IGN

        This is another standard signal handler, which will simply
        ignore the given signal.

As it looks it does not matter in this case, because when a process
terminates it will still communicate its exit status to Popen. But
what if I want for certain Popen signals SIG_IGN and others SIG_DFL.
How should I do that?

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof



More information about the Python-list mailing list