Main-process doesn't wait for child-processes

Jeff Epler jepler at unpythonic.net
Thu Apr 22 19:51:23 CEST 2004


Keep track of the processes you create, then wait for them each to exit.
Untested code:

    def do(task):
        pass

    processes = []
    try:
        for task in tasks:
            pid = os.fork()
            if pid == 0:
                do(task)
                os._exit(0)
            else:
                processes.append(pid)
    finally:
        while processes:
            pid = waitpid(0,0) # Wait for any child, block
            # print "reaped", pid
            processes.remove(pid)

Instead of looping until 'processes' is empty, you could wait until
waitpid raises os.error with errno==ECHILD ("No child processes").

Jeff




More information about the Python-list mailing list