spawning pyhon apps...

Mark Wooding mdw at
Sat Jan 10 01:24:53 CET 2009

bruce <bedouglas at> wrote:

> toying with an idea.. trying to figure out a good/best way to spawn
> multiple python scripts from a parent python app. i'm trying to figure
> out how to determine when all child apps have completed, or to
> possibly determine if any of the child processes have died/halted..

You don't say what platform you're using, but you mention os.waitpid so
I'll randomly assume it's Unix-like.

> do i iterate through a os.waitpid(pid) for each pid of the child
> processes i create?

That will technically work, and give you the information you wanted,
though not necessarily in the most timely fashion.  It'll block on each
process in turn, waiting for its exit status -- so it'll finish as soon
as all the children are dead, but if (say) the fifth process dies first,
you won't find out until the first four have also passed on.

If you don't have anything better for your program to do, and you're
really on Unix, you can call

  kid, status = os.waitpid(0, 0)

to wait for something to happen to any of your process's children; the
kid is the process-id of the child being reported and the status is what
happened to it.

If you do have other things for your process to be doing, then your best
bet is to establish a signal handler for SIGCHLD and installing a
handler of the form

  import signal as S
  import os as OS
  import errno as E

  ## Children sometimes die.  It's sad.
  def sigchld(sig, frame):
      while True:
	kid, status = OS.waitpid(0, OS.WNOHANG)
	if kid == 0:
        ## Handle death of KID here.
    except OSError, err:
      if err.errno != E.ECHILD:

  ### ...

  ## Establish handler.
  S.signal(S.SIGCHLD, sigchld)

should work.

If you're running on Windows then these tricks won't work.  As a grim
hack, you could start a thread per child process and have each thread
wait for its own child (sending the exit status through a queue or
something).  I'm afraid I don't know Windows well enough to offer
slicker solutions; maybe someone else can help.

-- [mdw]

More information about the Python-list mailing list