Ack! Zombie processes won't die!

Jp Calderone exarkun at divmod.com
Wed Dec 22 16:49:36 CET 2004



On Wed, 22 Dec 2004 15:37:18 GMT, Brian <thisisnotmyreal at ddress.com> wrote:
>>From one script, I'm spawnv'ing another that will launch mpg123 to play a 
> specified mp3.  Problem is that After the second script has launched 
> mpg123, it'll turn into a zombie process.  It doesn't happen when I launch 
> it from the command line, so there's something wrong with the way I'm 
> calling it (I believe).
> 
> mp3pid = os.spawnv(os.P_NOWAIT, "/oter/playfile.py", ["playfile", filename, 
> "0"])
> 
> Shouldn't this launch the script without waiting for it to finish?

  Yes.  But since you told it not to wait on the child process, you 
are now responsible for waiting on it.  Child processes are zombies 
until their parent "reaps" them.  This is done with the wait() 
function:

    pid, status = os.wait()

  or the waitpid() function:

    pid, status = os.waitpid(mp3pid, options)

  Of course, both of these block.  When a child process exits, the parent 
receives SIGCHLD.  You could install a signal handler for this and only 
call os.wait/pid() then.  Of course, signal delivery is unreliable, so 
this could still leave you with zombie processes.  You could use 
os.waitpid() with the WNOHANG option to prevent it from blocking and call 
it periodically.  This would leave you with a zombie for a short while, but
then clean it up.  You could combine signal handling and polling to completely 
minimize the time the zombie exists.

  You could also use something other than os.spawnv().  The new subprocess 
module may have something that simplifies this (I haven't looked at it in 
detail yet).  Twisted's spawnProcess() might also be worth a look, as it 
does take care of waiting at the appropriate time.

  Jp



More information about the Python-list mailing list