How to Check if a Child Process is Still Running in Python

The solution to this I found online was:
import os, errno
def PidIsAlive(pid):
try:
os.kill(pid, 0)
return True
except OSError, e:
return e.errno == errno.EPERM

However, it fails if the process is a zombie. This is a much better solution if the process is a child:
import os, errno
def PidIsAlive(pid):
try:
return os.waitpid(pid, os.WNOHANG) == (0, 0)
except OSError, e:
if e.errno != errno.ECHILD:
raise
Granted, this is exposed through subprocess.Popen.poll(). However, poll() doesn't allow you to check on child processes of the Popen object.

No comments:

Post a Comment