Example #1
0
    def receive(self, blocking=True):
        with Timeout.DisableAlarm():
            if not blocking:
                return self.incoming.get(block=False)

            difference = Timeout.difference()
            if difference is not None:
                return self.incoming.get(timeout=difference)
            return self.incoming.get()
Example #2
0
    def run(*popenargs, **kwargs):
        timeout = kwargs.pop('timeout', None)

        with Timeout.DisableAlarm():
            current_time = time.time()
            Timeout.check(current_time=current_time)
            difference = Timeout.difference(current_time=current_time)

            if difference:
                timeout = min(timeout or sys.maxsize, int(math.ceil(difference)))
            return subprocess.run(*popenargs, timeout=timeout, **kwargs)
Example #3
0
    def run(*popenargs, **kwargs):
        timeout = kwargs.pop('timeout', None)
        capture_output = kwargs.pop('capture_output', False)

        with Timeout.DisableAlarm():
            current_time = time.time()
            Timeout.check(current_time=current_time)
            difference = Timeout.difference(current_time=current_time)

            if difference:
                timeout = min(timeout or sys.maxsize, int(math.ceil(difference)))
            if capture_output:
                if ('stdout' in kwargs) or ('stderr' in kwargs):
                    raise ValueError('stdout and stderr arguments may not be used with capture_output.')
                kwargs['stdout'] = subprocess.PIPE
                kwargs['stderr'] = subprocess.PIPE
            return subprocess.run(*popenargs, timeout=timeout, **kwargs)
Example #4
0
        def wait(self):
            if self.poll() is not None:
                return

            # Need to check the timeout context
            timeout = Timeout.difference()
            if timeout and (self._completion.elapsed is None
                            or timeout < self._completion.elapsed):
                raise TimeoutExpired(self._args, timeout)

            if self._completion.elapsed is None:
                raise ValueError(
                    'Running a command that hangs without a timeout')

            if self._completion.elapsed:
                time.sleep(self._completion.elapsed)

            self.returncode = self._completion.returncode
            if self.stdout:
                self.stdout.seek(0)
            if self.stderr:
                self.stderr.seek(0)
Example #5
0
 def test_difference(self):
     with mocks.Time:
         with Timeout(1):
             self.asssertIsClose(Timeout.difference(), 1)