process and spinning slash
bellman at lysator.liu.se
Sun Oct 30 19:23:49 CET 2005
aleaxit at yahoo.com (Alex Martelli) writes:
> Have the spin function accept the pid argument and exit the loop if said
> pid has terminated; to check the latter, e.g., os.kill(pid, 0) -- this
> will raise an OSError if no process with that pid exists, so you can use
> a try/except OSError: to catch that and break as appropriate.
Even after the process has exited, it will exist as a zombie until
you os.wait() och os.waitpid() for it. Sending signal 0 to it
will thus always succeed.
If you do wait for the child process, so the zombie disappears,
the process id can be reused by the OS. Some Unixes reuse pids
very quickly (if I remember correctly, AIX do so), and others more
slowly, but there is *always* the risk of the pid being reused
immediately after the zombie was removed. Of course, if you wait
for the process, you will know that it has died and don't need to
check by sending a signal to it if it is alive...
The best way to poll for the termination of a child process, is
dead_child, status = os.waitpid(pid, os.WNOHANG)
if dead_child == 0:
print "No child has died"
elif dead_child == pid:
print "The child process we waited for has died"
print "Some other child process has died"
The last branch can't happen if you wait for a specific process.
(Process ids <= 0 given to waitpid have special meanings; read
the manual page for waitpid(2) for those.)
If the child process was started using subprocess.Popen, you
should usually use the poll() methods on the Popen object to
check if the process has terminated.
Thomas Bellman, Lysator Computer Club, Linköping University, Sweden
"You are in a twisty little passage of ! bellman @ lysator.liu.se
standards, all conflicting." ! Make Love -- Nicht Wahr!
More information about the Python-list