Spawning and waiting for child processes in Python

Posted by Brendan Long on Stack Overflow See other posts from Stack Overflow or by Brendan Long
Published on 2010-06-07T21:59:14Z Indexed on 2010/06/07 22:02 UTC
Read the original article Hit count: 260

Filed under:
|
|
|

The relevant part of the code looks like this:

pids = [] 
for size in SIZES:
    pids.append(os.spawnv(os.P_NOWAIT, RESIZECMD, [RESIZECMD, lotsOfOptions]))

# Wait for all spawned imagemagick processes to finish
while pids:
    (pid, status) = os.waitpid(0, 0)
    if pid:
        pids.remove(pid)

What this should be doing is spawning all of the processes off, then waiting for each process to finish before continuing. What it does is work for the most part but sometimes crash on the next section (when it expects all of these processes to be finished).

Is there something wrong with this? Is there a better way of doing it?

The environment it has to work on is CentOS with Python 2.4, but I'm testing on Cygwin with Python 2.5, so it could be that it fails on my machine but will work on the Linux one (the Linux machine is very slow and this error is rare, so I haven't been able to get it on there).

© Stack Overflow or respective owner

Related posts about python

Related posts about linux