Forking python, defunct child
Asked Answered
L

3

7

I have some troubles with Python child processes so I wrote a very simple script:

import os
import sys
import time

pid = os.fork()
if pid:
    #parent
    time.sleep(30)
else:
    #child
    #os._exit(0)
    sys.exit()

While parent process is sleeping I launch

ps fax | grep py[t]hon

And I read this output

2577 ?        S      0:00 python /home/pi/python/GPIO/GPIODaemon.py restart
2583 ?        Z      0:00  \_ [python] <defunct>

Using sys.exit()oros._exit(0) there is always a Zombie process and I'm unable to understand why.

Working on my more complex code I was thinking that there was some resources that child processes were keeping locked, but on this simplified code child has no file/socket/db connection at all! Why is child process zombiefied?

Large answered 6/8, 2013 at 20:50 Comment(0)
L
11

To clear the child process in Unix you need to wait on the child, check one of the os.wait(), os.waitpid(), os.wait3() or os.wait4() at http://docs.python.org/2/library/os.html#os.wait

As to why this is so, this is a design decision of Unix. The child process keeps its return value in its process state, if it was to disappear you'll have no return value. The os.wait() also returns to you the return value and then the child process is released and all associated resources are released.

Levkas answered 6/8, 2013 at 20:54 Comment(3)
I fork the main process just to avoid waiting. Reading docs.python.org/2/library/os.html#os.wait seems that parent process will sleep until child is over.Large
I found a solution by myself, but I'm not really happy of it: I switch parent and child code. So now parent process ends (w/o zombie!) and the child becomes the new main process obviously with another pid (ugly). Everything is working fine but it isn't a good programming code.Large
Revering parent and child is not a good idea, you can use os.wait4 or os.waitpid and use os.WNOHANG option to do a non-blocking wait. Read on the equivalent OS syscall with man to learn the underlying concerns and methods of use.Levkas
H
0

I just had a similar problem: A process started by spawnl, which might end or might need to be terminated at a specific point. My solution to not have all the zombie processes was

def cleanup_subprocesses(self, pid):
  try:
    os.kill(pid, signal.SIGKILL)
  except OSError:
    pass
  os.waitpid(self._pid, 0)

If the process did not end in time, it gets killed, in any case, the waitpid-command is executed.

This does obviously not help, if there is no good point in your program, where you know, that you don’t need the process anymore.

Hammers answered 27/11, 2013 at 12:35 Comment(0)
G
0

all you need is just to write your custom SIGCHILD handler in parent:

import signal
import os

def sigchld_handler(signum, frame):
    # we just need to wait for it...
    pid, _ = os.waitpid(-1, os.WNOHANG)

signal.signal(signal.SIGCHLD, sigchld_handler)

so child would be waited right on termination and parent would not get locked until child's termination.

and don't forget to do

import sys
sys.exit(0)

in child

Gamesmanship answered 11/2, 2023 at 12:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.