1 # Copyright 1999-2012 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 from _emerge.PollScheduler import PollScheduler
6 class QueueScheduler(PollScheduler):
9 Add instances of SequentialTaskQueue and then call run(). The
10 run() method returns when no tasks remain.
13 def __init__(self, max_jobs=None, max_load=None):
14 PollScheduler.__init__(self)
19 self._max_jobs = max_jobs
20 self._max_load = max_load
23 self._schedule_listeners = []
26 self._queues.append(q)
29 self._queues.remove(q)
32 for q in self._queues:
35 def run(self, timeout=None):
37 timeout_callback = None
38 if timeout is not None:
39 def timeout_callback():
40 timeout_callback.timed_out = True
42 timeout_callback.timed_out = False
43 timeout_callback.timeout_id = self.sched_iface.timeout_add(
44 timeout, timeout_callback)
49 while self._keep_scheduling() and \
50 not (timeout_callback is not None and
51 timeout_callback.timed_out):
52 # We don't have any callbacks to trigger _schedule(),
53 # so we have to call it explicitly here.
55 self.sched_iface.iteration()
57 while self._is_work_scheduled() and \
58 not (timeout_callback is not None and
59 timeout_callback.timed_out):
60 self.sched_iface.iteration()
62 if timeout_callback is not None:
63 self.sched_iface.unregister(timeout_callback.timeout_id)
65 def _schedule_tasks(self):
68 @returns: True if there may be remaining tasks to schedule,
71 if self._terminated_tasks:
74 while self._can_add_job():
75 n = self._max_jobs - self._running_job_count()
79 if not self._start_next_job(n):
82 def _keep_scheduling(self):
83 return not self._terminated_tasks and any(self._queues)
85 def _running_job_count(self):
87 for q in self._queues:
88 job_count += len(q.running_tasks)
89 self._jobs = job_count
92 def _start_next_job(self, n=1):
94 for q in self._queues:
95 initial_job_count = len(q.running_tasks)
97 final_job_count = len(q.running_tasks)
98 if final_job_count > initial_job_count:
99 started_count += (final_job_count - initial_job_count)
100 if started_count >= n: