Commit 9f6191db authored by Stefan Scherfke's avatar Stefan Scherfke
Browse files

More improvements/fixes for the Scheduler and its documentation. The...

More improvements/fixes for the Scheduler and its documentation. The benchmarks run also slightly faster (but this could also be due to measuring inaccurarcies).
parent a50db16d
......@@ -130,6 +130,14 @@
.. autoclass:: Scheduler
.. attribute:: env
The :class:`Environment` that the scheduler is associated with.
.. attribute:: now
The current simulation time.
.. attribute:: queue
A list with all currently scheduled events.
......@@ -518,18 +518,19 @@ class EmptySchedule(Exception):
class Scheduler(object):
"""Schedulers manage the event queue of an :class:`Environment`.
They schedule/enqueue new events and pop events from the queue.
They schedule/enqueue new events and pop events from the queue. They also
manage the current simulation time.
def __init__(self, env):
def __init__(self, env, initial_time):
self.env = env
self.eid = count() = initial_time
self.queue = [] = 0
self._eid = count()
def schedule(self, event, priority, delay=0):
"""Schedule an *event* with a given *priority* and a *delay*."""
heappush(self.queue, ( + delay, priority, next(self.eid),
heappush(self.queue, ( + delay, priority, next(self._eid),
def peek(self):
......@@ -540,10 +541,10 @@ class Scheduler(object):
except IndexError:
return Infinity
def fetch(self):
"""Remove and returns the next event from the queue.
def pop(self):
"""Remove and return the next event from the queue as ``(now, event)``.
This method advances the simulation time of the environment.
Raise :exc:`EmptySchedule` if the schedule is empty.
......@@ -559,11 +560,10 @@ class Environment(object):
def __init__(self, initial_time=0, scheduler=None):
self._now = initial_time
self._active_proc = None
if scheduler is None:
scheduler = Scheduler(self)
scheduler = Scheduler(self, initial_time)
self.scheduler = scheduler
self.event = types.MethodType(Event, self)
......@@ -573,7 +573,7 @@ class Environment(object):
self.start = types.MethodType(Process, self)
self.schedule = self.scheduler.schedule
self.fetch = self.scheduler.fetch
self.pop = self.scheduler.pop
def active_process(self):
......@@ -611,7 +611,7 @@ def step(env):
Raise an :exc:`IndexError` if no valid event is on the heap.
event = env.fetch()
event = env.pop()
# Process callbacks of the event.
for callback in event.callbacks:
......@@ -16,15 +16,15 @@ Infinity = float('inf')
class RealtimeScheduler(Scheduler):
def __init__(self, env, sim_start, factor, strict):
Scheduler.__init__(self, env)
self.sim_start = sim_start
def __init__(self, env, initial_time, factor, strict):
Scheduler.__init__(self, env, initial_time)
self.sim_start = initial_time
self.real_start = time()
self.factor = factor
self.strict = strict
def fetch(self):
event = Scheduler.fetch(self)
def pop(self):
event = super(RealtimeScheduler, self).pop()
sim_delta = - self.sim_start
real_delta = time() - self.real_start
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment