#NEWLINE# def wait_pid( pid , timeout = None , callback = None ) : #NEWLINE# #INDENT# "Wait for process with pid 'pid' to terminate and return its\n    exit status code as an integer.\n\n    If pid is not a children of os.getpid() (current process) just\n    waits until the process disappears and return None.\n\n    If pid does not exist at all return None immediately.\n\n    Raise TimeoutExpired on timeout expired (if specified).\n    " #NEWLINE# def check_timeout( delay ) : #NEWLINE# #INDENT# if ( timeout is not None ) : #NEWLINE# #INDENT# if ( time . time ( ) >= stop_at ) : #NEWLINE# #INDENT# if callback : #NEWLINE# #INDENT# callback ( pid ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise TimeoutExpired #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# time . sleep ( delay ) #NEWLINE# return min ( ( delay * 2 ) , 0.04 ) #NEWLINE# #UNINDENT# if ( timeout is not None ) : #NEWLINE# #INDENT# waitcall = ( lambda : os . waitpid ( pid , os . WNOHANG ) ) #NEWLINE# stop_at = ( time . time ( ) + timeout ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# waitcall = ( lambda : os . waitpid ( pid , 0 ) ) #NEWLINE# #UNINDENT# delay = 0.0001 #NEWLINE# while 1 : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# ( retpid , status ) = waitcall ( ) #NEWLINE# #UNINDENT# except OSError as err : #NEWLINE# #INDENT# if ( err . errno == errno . EINTR ) : #NEWLINE# #INDENT# delay = check_timeout ( delay ) #NEWLINE# continue #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( err . errno == errno . ECHILD ) : #NEWLINE# #INDENT# while 1 : #NEWLINE# #INDENT# if pid_exists ( pid ) : #NEWLINE# #INDENT# delay = check_timeout ( delay ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( retpid == 0 ) : #NEWLINE# #INDENT# delay = check_timeout ( delay ) #NEWLINE# continue #NEWLINE# #UNINDENT# if os . WIFSIGNALED ( status ) : #NEWLINE# #INDENT# return os . WTERMSIG ( status ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if os . WIFEXITED ( status ) : #NEWLINE# #INDENT# return os . WEXITSTATUS ( status ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise RuntimeError ( 'unknown process exit status' )