PollScheduler: use timeout for loadavg checks
authorZac Medico <zmedico@gentoo.org>
Tue, 21 Aug 2012 03:29:04 +0000 (20:29 -0700)
committerZac Medico <zmedico@gentoo.org>
Tue, 21 Aug 2012 03:29:04 +0000 (20:29 -0700)
This ensures that the code from commit
fe03b5fd790fc09bd6594c55bb174e80aaac1e5a behaves correctly even when
there are no poll events arriving for a long time (which might happen
if the running builds don't produce output for a long time).

pym/_emerge/PollScheduler.py

index 5103e31d60054daf9fd6800d8f1f86c820b4e57a..808fa6e1fcc529ee50e51c79a3e96fec4b0784c6 100644 (file)
@@ -20,6 +20,9 @@ from _emerge.getloadavg import getloadavg
 
 class PollScheduler(object):
 
+       # max time between loadavg checks (milliseconds)
+       _loadavg_latency = 30000
+
        class _sched_iface_class(SlotObject):
                __slots__ = ("IO_ERR", "IO_HUP", "IO_IN", "IO_NVAL", "IO_OUT",
                        "IO_PRI", "child_watch_add",
@@ -147,23 +150,24 @@ class PollScheduler(object):
 
        def _main_loop(self):
                term_check_id = self.sched_iface.idle_add(self._termination_check)
+               loadavg_check_id = None
+               if self._max_load is not None:
+                       # We have to schedule periodically, in case the load
+                       # average has changed since the last call.
+                       loadavg_check_id = self.sched_iface.timeout_add(
+                               self._loadavg_latency, self._schedule)
+
                try:
                        # Populate initial event sources. Unless we're scheduling
                        # based on load average, we only need to do this once
                        # here, since it can be called during the loop from within
                        # event handlers.
                        self._schedule()
-                       max_load = self._max_load
 
                        # Loop while there are jobs to be scheduled.
                        while self._keep_scheduling():
                                self.sched_iface.iteration()
 
-                               if max_load is not None:
-                                       # We have to schedule periodically, in case the load
-                                       # average has changed since the last call.
-                                       self._schedule()
-
                        # Clean shutdown of previously scheduled jobs. In the
                        # case of termination, this allows for basic cleanup
                        # such as flushing of buffered output to logs.
@@ -171,6 +175,8 @@ class PollScheduler(object):
                                self.sched_iface.iteration()
                finally:
                        self.sched_iface.source_remove(term_check_id)
+                       if loadavg_check_id is not None:
+                               self.sched_iface.source_remove(loadavg_check_id)
 
        def _is_work_scheduled(self):
                return bool(self._running_job_count())