From 4a858889202448e93cb43ec2559e9f66c30b09c9 Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Fri, 17 Feb 2012 02:20:35 -0800 Subject: EventLoop.iteration: run timeouts last Run timeouts last, in order to minimize latency in termination of iteration loops that they may control. --- pym/portage/util/_eventloop/EventLoop.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/pym/portage/util/_eventloop/EventLoop.py b/pym/portage/util/_eventloop/EventLoop.py index 808c83d4c..475a4a9c6 100644 --- a/pym/portage/util/_eventloop/EventLoop.py +++ b/pym/portage/util/_eventloop/EventLoop.py @@ -130,10 +130,12 @@ class EventLoop(object): pass if self._run_timeouts(): events_handled += 1 - if not event_handlers: - return bool(events_handled) - else: - return bool(events_handled) + + # If any timeouts have executed, then return immediately, + # in order to minimize latency in termination of iteration + # loops that they may control. + if events_handled or not event_handlers: + return bool(events_handled) if not event_queue: @@ -149,17 +151,10 @@ class EventLoop(object): else: timeout = 0 - if self._run_timeouts(): - events_handled += 1 - try: - self._poll(timeout=timeout) except StopIteration: - # This could happen if there are no IO event handlers - # after _poll() calls _run_timeouts(), due to them - # being removed by timeout or idle callbacks. It can - # also be triggered by EINTR which is caused by signals. + # This can be triggered by EINTR which is caused by signals. pass # NOTE: IO event handlers may be re-entrant, in case something @@ -172,6 +167,11 @@ class EventLoop(object): if not x.callback(f, event, *x.args): self.source_remove(x.source_id) + # Run timeouts last, in order to minimize latency in + # termination of iteration loops that they may control. + if self._run_timeouts(): + events_handled += 1 + return bool(events_handled) def child_watch_add(self, pid, callback, data=None): -- cgit v1.2.3-1-g7c22