'Wait for the process to terminate. Returns returncode attribute.
If timeout seconds are reached and the process has not terminated,
it will be forcefully killed. If timeout is -1, wait will not
time out.'

def wait(self, timeout=None, group=True):

    if (timeout is not None):
        timeout = (timeout * 1000)
    if (self.returncode is not None):
        return self.returncode
    starttime = datetime.datetime.now()
    if mswindows:
        if (timeout is None):
            timeout = (-1)
        rc = winprocess.WaitForSingleObject(self._handle, timeout)
        if (rc != winprocess.WAIT_TIMEOUT):
            def check():
                now = datetime.datetime.now()
                diff = (now - starttime)
                if ((((diff.seconds * 1000) * 1000) + diff.microseconds) < (timeout * 1000)):
                    if self._job:
                        if (winprocess.QueryInformationJobObject(self._job, 8)['BasicInfo']['ActiveProcesses'] > 0):
                            return True
                    else:
                        return True
                return False
            while check():
                time.sleep(0.5)
        now = datetime.datetime.now()
        diff = (now - starttime)
        if ((((diff.seconds * 1000) * 1000) + diff.microseconds) > (timeout * 1000)):
            self.kill(group)
        else:
            self.returncode = winprocess.GetExitCodeProcess(self._handle)
    else:
        if ((sys.platform == 'linux2') or (sys.platform in ('sunos5', 'solaris'))):
            def group_wait(timeout):
                try:
                    os.waitpid(self.pid, 0)
                except OSError as e:
                    pass
                return self.returncode
        elif (sys.platform == 'darwin'):
            def group_wait(timeout):
                try:
                    count = 0
                    if ((timeout is None) and self.kill_called):
                        timeout = 10
                    if (timeout is None):
                        while 1:
                            os.killpg(self.pid, signal.SIG_DFL)
                    while ((count * 2) <= timeout):
                        os.killpg(self.pid, signal.SIG_DFL)
                        time.sleep(0.5)
                        count += 500
                except OSError:
                    return self.returncode
        if (timeout is None):
            if (group is True):
                return group_wait(timeout)
            else:
                subprocess.Popen.wait(self)
                return self.returncode
        returncode = False
        now = datetime.datetime.now()
        diff = (now - starttime)
        while (((((diff.seconds * 1000) * 1000) + diff.microseconds) < (timeout * 1000)) and (returncode is False)):
            if (group is True):
                return group_wait(timeout)
            elif (subprocess.poll() is not None):
                returncode = self.returncode
            time.sleep(0.5)
            now = datetime.datetime.now()
            diff = (now - starttime)
        return self.returncode
    return self.returncode
