为什么Python在POSIX上不使用signal和EINTR进行超时?

时间:2018-07-31 13:38:33

标签: subprocess signals

这是Popen.wait(...)的现代Python POSIX摘录:

def _wait(self, timeout):
    """Internal implementation of wait() on POSIX."""
    if self.returncode is not None:
        return self.returncode

    if timeout is not None:
        endtime = _time() + timeout
        # Enter a busy loop if we have a timeout.  This busy loop was
        # cribbed from Lib/threading.py in Thread.wait() at r71065.
        delay = 0.0005 # 500 us -> initial delay of 1 ms
        while True:
            if self._waitpid_lock.acquire(False):
                try:
                    if self.returncode is not None:
                        break  # Another thread waited.
                    (pid, sts) = self._try_wait(os.WNOHANG)
                    assert pid == self.pid or pid == 0
                    if pid == self.pid:
                        self._handle_exitstatus(sts)
                        break
                finally:
                    self._waitpid_lock.release()
            remaining = self._remaining_time(endtime)
            if remaining <= 0:
                raise TimeoutExpired(self.args, timeout)
            delay = min(delay * 2, remaining, .05)
            time.sleep(delay)
    else:
        # ...
    return self.returncode

为什么Python不发送信号(SIGALARM)并在EINTR上进行检查,却不进行挂起等待,睡眠和哑巴重试?

我猜这是因为设置信号处理程序对于应用程序可能是相当麻烦的,但是Python确实可以处理SIGINT ...

也许它不够便携?

在Windows上,看起来很流畅:WaitForSingleObject超时。

0 个答案:

没有答案