(pid, timeout=None)
Wait for process with pid 'pid' to terminate and return its
    exit status code as an integer.

    If pid is not a children of os.getpid() (current process) just
    waits until the process disappears and return None.

    If pid does not exist at all return None immediately.

    Raise TimeoutExpired on timeout expired.
Wait for process with pid 'pid' to terminate and return its
    exit status code as an integer.
Wait for process with pid pid to terminate and return its exit status code as an integer .
def wait_pid(pid, timeout=None):
    """Wait for process with pid 'pid' to terminate and return its
    exit status code as an integer.

    If pid is not a children of os.getpid() (current process) just
    waits until the process disappears and return None.

    If pid does not exist at all return None immediately.

    Raise TimeoutExpired on timeout expired.
    """
    def check_timeout(delay):
        if timeout is not None:
            if timer() >= stop_at:
                raise TimeoutExpired()
        time.sleep(delay)
        return min(delay * 2, 0.04)

    timer = getattr(time, 'monotonic', time.time)
    if timeout is not None:
        def waitcall():
            return os.waitpid(pid, os.WNOHANG)
        stop_at = timer() + timeout
    else:
        def waitcall():
            return os.waitpid(pid, 0)

    delay = 0.0001
    while True:
        try:
            retpid, status = waitcall()
        except OSError as err:
            if err.errno == errno.EINTR:
                delay = check_timeout(delay)
                continue
            elif err.errno == errno.ECHILD:
                # This has two meanings:
                # - pid is not a child of os.getpid() in which case
                #   we keep polling until it's gone
                # - pid never existed in the first place
                # In both cases we'll eventually return None as we
                # can't determine its exit status code.
                while True:
                    if pid_exists(pid):
                        delay = check_timeout(delay)
                    else:
                        return
            else:
                raise
        else:
            if retpid == 0:
                # WNOHANG was used, pid is still running
                delay = check_timeout(delay)
                continue
            # process exited due to a signal; return the integer of
            # that signal
            if os.WIFSIGNALED(status):
                return os.WTERMSIG(status)
            # process exited using exit(2) system call; return the
            # integer exit(2) system call has been called with
            elif os.WIFEXITED(status):
                return os.WEXITSTATUS(status)
            else:
                # should never happen
                raise RuntimeError("unknown process exit status")
def wait_pid ( pid , timeout = None ) : def check_timeout ( delay ) : if timeout is not None : if timer ( ) >= stop_at : raise TimeoutExpired ( ) time . sleep ( delay ) return min ( delay * 2 , 0.04 ) timer = getattr ( time , 'monotonic' , time . time ) if timeout is not None : def waitcall ( ) : return os . waitpid ( pid , os . WNOHANG ) stop_at = timer ( ) + timeout else : def waitcall ( ) : return os . waitpid ( pid , 0 ) delay = 0.0001 while True : try : retpid , status = waitcall ( ) except OSError as err : if err . errno == errno . EINTR : delay = check_timeout ( delay ) continue elif err . errno == errno . ECHILD : # This has two meanings: # - pid is not a child of os.getpid() in which case #   we keep polling until it's gone # - pid never existed in the first place # In both cases we'll eventually return None as we # can't determine its exit status code. while True : if pid_exists ( pid ) : delay = check_timeout ( delay ) else : return else : raise else : if retpid == 0 : # WNOHANG was used, pid is still running delay = check_timeout ( delay ) continue # process exited due to a signal; return the integer of # that signal if os . WIFSIGNALED ( status ) : return os . WTERMSIG ( status ) # process exited using exit(2) system call; return the # integer exit(2) system call has been called with elif os . WIFEXITED ( status ) : return os . WEXITSTATUS ( status ) else : # should never happen raise RuntimeError ( "unknown process exit status" )