Home | Articles | CV (pdf | short)
<2014-01-23> by Lorenzo

Die zombie, die!

The subprocess module in Python standard library allows you to spanw processes to do some tasks. The parent process should wait on its children processes in order get the return value of the process and avoid zombies.

Sometimes, though, processes are particularly hard to kill and even after a wait, a terminate or a kill they refuse to die: they become zombies.

In theory, Python should take care of reaping zombies for you once the zombie process gets garbage collected. In practice, it does not seem to work reliably and bigger weapons are needed.

Take for example the following script:

Here I create 3 processes, without holding any references to them, wait for them to finish, and I would expect the GC to kick in and reap them when done. Instead, they are left as zombies until the parent process exits.

Forcing GC does not work either:

One way is to explicitely del them:

Another way is to explicitly cycle over all the children of the main process and os.waitpid on them: this has the effect of removing the zombies from the process table: