Esempio n. 1
0
    def wait(self, timeout=None, group=True):
        """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."""

        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:
                while (starttime -
                       datetime.datetime.now()).microseconds < timeout or (
                           winprocess.QueryInformationJobObject(
                               self._job, 8)['BasicInfo']['ActiveProcesses'] >
                           0):
                    time.sleep(.5)

            if (starttime - datetime.datetime.now()).microseconds > timeout:
                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, e:
                        pass  # If wait has already been called on this pid, bad things happen
                    return self.returncode
            elif sys.platform == 'darwin':

                def group_wait(timeout):
                    try:
                        count = 0
                        if timeout is None and self.kill_called:
                            timeout = 10  # Have to set some kind of timeout or else this could go on forever
                        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(.5)
                            count += .5
                    except exceptions.OSError:
                        return self.returncode
Esempio n. 2
0
 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
Esempio n. 3
0
 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):
                 # Job Object is still containing active processes
                 return 1
         else:
             # No job, we use GetExitCodeProcess, which will tell us if the process is still active
             self.returncode = winprocess.GetExitCodeProcess(self._handle)
             if (self.returncode == winprocess.STILL_ACTIVE):
                 # Process still active, continue waiting
                 return 1
         # Process not active, return 0
         return 0
     else:
         # Timed out, return -1
         return -1
Esempio n. 4
0
    def wait(self, timeout=None, group=True):
        """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."""

        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:
                while (starttime -
                       datetime.datetime.now()).microseconds < timeout or (
                           winprocess.QueryInformationJobObject(
                               self._job, 8)['BasicInfo']['ActiveProcesses'] >
                           0):
                    time.sleep(.5)

            if (starttime - datetime.datetime.now()).microseconds > timeout:
                self.kill(group)
            else:
                self.returncode = winprocess.GetExitCodeProcess(self._handle)
        else:
            if sys.platform == 'linux2' or sys.platform == 'cygwin':

                def group_wait():
                    os.waitpid(self.pid, 0)
                    return self.returncode
            elif sys.platform == 'darwin':

                def group_wait():
                    try:
                        while 1:
                            os.killpg(self.pid, signal.SIG_DFL)
                            time.sleep(.5)
                    except exceptions.OSError:
                        return self.returncode

            if timeout is None:
                if group is True:
                    return group_wait()
                else:
                    subprocess.Popen.wait(self)
                    return self.returncode

            returncode = False

            while (starttime - datetime.datetime.now()
                   ).microseconds < timeout or (returncode is False):
                if group is True:
                    return group_wait()
                else:
                    if subprocess.poll() is not None:
                        returncode = self.returncode
                time.sleep(.5)
            return self.returncode

        return self.returncode
Esempio n. 5
0
        def _execute_child(self, args, executable, preexec_fn, close_fds, cwd,
                           env, universal_newlines, startupinfo, creationflags,
                           shell, p2cread, p2cwrite, c2pread, c2pwrite,
                           errread, errwrite):
            if not isinstance(args, types.StringTypes):
                args = subprocess.list2cmdline(args)

            if startupinfo is None:
                startupinfo = winprocess.STARTUPINFO()

            if None not in (p2cread, c2pwrite, errwrite):
                startupinfo.dwFlags |= winprocess.STARTF_USESTDHANDLES

                startupinfo.hStdInput = int(p2cread)
                startupinfo.hStdOutput = int(c2pwrite)
                startupinfo.hStdError = int(errwrite)
            if shell:
                startupinfo.dwFlags |= winprocess.STARTF_USESHOWWINDOW
                startupinfo.wShowWindow = winprocess.SW_HIDE
                comspec = os.environ.get("COMSPEC", "cmd.exe")
                args = comspec + " /c " + args

            # We create a new job for this process, so that we can kill
            # the process and any sub-processes
            self._job = winprocess.CreateJobObject()

            creationflags |= winprocess.CREATE_SUSPENDED
            creationflags |= winprocess.CREATE_UNICODE_ENVIRONMENT

            hp, ht, pid, tid = winprocess.CreateProcess(
                executable,
                args,
                None,
                None,  # No special security
                1,  # Must inherit handles!
                creationflags,
                winprocess.EnvironmentBlock(env),
                cwd,
                startupinfo)

            self._child_created = True
            self._handle = hp
            self._thread = ht
            self.pid = pid
            self.tid = tid

            winprocess.AssignProcessToJobObject(self._job, hp)
            winprocess.ResumeThread(ht)

            if p2cread is not None:
                p2cread.Close()
            if c2pwrite is not None:
                c2pwrite.Close()
            if errwrite is not None:
                errwrite.Close()
            time.sleep(.1)
            p = winprocess.QueryInformationJobObject(
                self._job, 8)['BasicInfo']['ActiveProcesses']
            if p is 0:
                self._job_working = False
            else:
                self._job_working = True