How to tell if a forked process is done?

Jeff Epler jepler at
Wed Sep 24 15:29:28 CEST 2003

os.waitpid() can tell whether a child has exited, and return its status
if so.  It can either enter a blocking wait, or it can return

>>> pid = os.spawnv(os.P_NOWAIT, "/bin/sleep", ["sleep", "30"])

With WNOHANG, it returns immediately.  The returned pid is 0 to show
that the process has not exited yet.
>>> os.waitpid(pid, os.WNOHANG)
(0, 0)

Wait for the process to return.  The second number is related to the
exit status and should be managed with os.WEXITSTATUS() etc.
>>> os.waitpid(pid, 0)
(29202, 0)

Waiting again produces a traceback (no danger from another process
created with the same pid)
>>> os.waitpid(pid, 0)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
OSError: [Errno 10] No child processes

If you want to use an argument list instead of an argument string, but
always want to wait for the program to complete, use os.spawn* with


More information about the Python-list mailing list