waste of resources ?

Carey Evans c.evans at clear.net.nz
Wed Jun 9 07:26:47 EDT 1999


Arne Mueller <a.mueller at icrf.icnet.uk> writes:


[snip]

> > def sigchild_handler(signum, frame):
> >     os.wait()

[snip]

> Hm, that doesn't work properly  with my program. THe number of zombie
> processes is steadily at about 20, which means there are 20 unhandled
> died children.  

Unix doesn't guarantee you'll get exactly one signal delivered per
event.  To be sure to get all the children, you need to loop over the
processes with waitpid().

I think something like this should do it:

----------
def sigchld_handler(signum, frame):
    print "Checking for processes..."
    try:
        while 1:
            pid, sts = os.waitpid(-1, os.WNOHANG)
            if pid == 0:
                break
            print "Process %d ended with status %04x." % (pid, sts)
    except OSError, detail:
        if detail.args[0] != errno.ECHILD:
            raise
        else:
            print "No more children."
    else:
        print "Some children yet to finish."
----------

I can't actually see any way to keep count well enough and avoid race
conditions, to get rid of the try/except.  Sometimes I wish Python had 
POSIX signals, but not badly enough to actually write it.

-- 
	 Carey Evans  http://home.clear.net.nz/pages/c.evans/

	     "I'm not a god.  I've just been misquoted."




More information about the Python-list mailing list