From f17ff92635818620ea0df156dee4796c353d954a Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Tue, 7 Feb 2012 19:58:48 -0800 Subject: [PATCH] AbstractPollTask: add _wait_loop method --- pym/_emerge/AbstractPollTask.py | 23 ++++++++++++++++++++++- pym/_emerge/SubProcess.py | 8 +++----- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/pym/_emerge/AbstractPollTask.py b/pym/_emerge/AbstractPollTask.py index 13e3c23f9..b4728e7a2 100644 --- a/pym/_emerge/AbstractPollTask.py +++ b/pym/_emerge/AbstractPollTask.py @@ -1,10 +1,11 @@ -# Copyright 1999-2011 Gentoo Foundation +# Copyright 1999-2012 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 import array import errno import logging import os +import time from portage.util import writemsg_level from _emerge.AsynchronousTask import AsynchronousTask @@ -121,3 +122,23 @@ class AbstractPollTask(AsynchronousTask): self._unregister() self.wait() + def _wait_loop(self, timeout=None): + + if timeout is None: + while self._registered: + self.scheduler.iteration() + return + + remaining_timeout = timeout + start_time = time.time() + while self._registered: + self.scheduler.iteration() + elapsed_time = time.time() - start_time + if elapsed_time < 0: + # The system clock has changed such that start_time + # is now in the future, so just assume that the + # timeout has already elapsed. + break + remaining_timeout = timeout - 1000 * elapsed_time + if remaining_timeout <= 0: + break diff --git a/pym/_emerge/SubProcess.py b/pym/_emerge/SubProcess.py index c3dc74a91..17a3e8f20 100644 --- a/pym/_emerge/SubProcess.py +++ b/pym/_emerge/SubProcess.py @@ -64,8 +64,7 @@ class SubProcess(AbstractPollTask): if self._registered: if self.cancelled: - timeout = self._cancel_timeout - self.scheduler.schedule(self._reg_id, timeout=timeout) + self._wait_loop(timeout=self._cancel_timeout) if self._registered: try: os.kill(self.pid, signal.SIGKILL) @@ -73,12 +72,11 @@ class SubProcess(AbstractPollTask): if e.errno != errno.ESRCH: raise del e - self.scheduler.schedule(self._reg_id, timeout=timeout) + self._wait_loop(timeout=self._cancel_timeout) if self._registered: self._orphan_process_warn() else: - while self._registered: - self.scheduler.iteration() + self._wait_loop() self._unregister() if self.returncode is not None: return self.returncode -- 2.26.2