From: Zac Medico Date: Thu, 16 Feb 2012 04:58:10 +0000 (-0800) Subject: EventLoop.iteration: sleep if no IO handlers X-Git-Tag: v2.2.0_alpha87~25 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=5697972e9f2a3021ca6ed99b24e23e32f7515bf9;p=portage.git EventLoop.iteration: sleep if no IO handlers Sleep so that we don't waste cpu time by looping too quickly. This makes EventLoop useful for code that needs to wait for timeout callbacks regardless of whether or not any IO handlers are currently registered. --- diff --git a/pym/portage/util/_eventloop/EventLoop.py b/pym/portage/util/_eventloop/EventLoop.py index f6f9fc54b..24ba14077 100644 --- a/pym/portage/util/_eventloop/EventLoop.py +++ b/pym/portage/util/_eventloop/EventLoop.py @@ -158,7 +158,19 @@ class EventLoop(object): if self._run_timeouts(): events_handled += 1 if not event_handlers: - return bool(events_handled) + if not events_handled and may_block and \ + self._timeout_interval is not None: + # Sleep so that we don't waste cpu time by looping too + # quickly. This makes EventLoop useful for code that needs + # to wait for timeout callbacks regardless of whether or + # not any IO handlers are currently registered. + time.sleep(self._timeout_interval/1000) + if self._run_timeouts(): + events_handled += 1 + if not event_handlers: + return bool(events_handled) + else: + return bool(events_handled) if not self._poll_event_queue: if may_block: