EventLoop.iteration: sleep if no IO handlers
authorZac Medico <zmedico@gentoo.org>
Thu, 16 Feb 2012 04:58:10 +0000 (20:58 -0800)
committerZac Medico <zmedico@gentoo.org>
Thu, 16 Feb 2012 04:58:10 +0000 (20:58 -0800)
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.

pym/portage/util/_eventloop/EventLoop.py

index f6f9fc54b2d659a3c27fe4228bba02931970567c..24ba14077318c4353942db8ee32a382a8e3d5ca0 100644 (file)
@@ -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: