1. Team SimPy
  2. Untitled project
  3. simpy

Commits

Stefan Scherfke  committed b5d8466

Merged Scheduler.enqueue() and Scheduler.schedule() to Scheduler.schedule() and improved the docstrings.

  • Participants
  • Parent commits 8909eee
  • Branches default

Comments (0)

Files changed (1)

File simpy/core.py

View file
 
         self.ok = True
         self._value = value
-        self.env.enqueue(DEFAULT_PRIORITY, self)
+        self.env.schedule(self, DEFAULT_PRIORITY)
 
     def fail(self, exception):
         """Schedule the event and mark it as failed.
             raise RuntimeError('%s has already been triggered' % self)
         self.ok = False
         self._value = exception
-        self.env.enqueue(DEFAULT_PRIORITY, self)
+        self.env.schedule(self, DEFAULT_PRIORITY)
 
     def __and__(self, other):
         return Condition(self.env, all_events, [self, other])
         self._delay = delay
         self.ok = True
         self._value = value
-        env.schedule(delay, LOW_PRIORITY, self)
+        env.schedule(self, LOW_PRIORITY, delay)
 
     def _desc(self):
         """Return a string *Timeout(delay[, value=value])*."""
         self.ok = True
         self._value = None
         self.callbacks = [process._resume]
-        env.enqueue(HIGH_PRIORITY, self)
+        env.schedule(self, HIGH_PRIORITY)
 
 
 class Process(Event):
         # Interrupts do not cause the simulation to crash.
         event.defused = True
         event.callbacks.append(self._resume)
-        self.env.enqueue(HIGH_PRIORITY, event)
+        self.env.schedule(event, HIGH_PRIORITY)
 
     def _resume(self, event):
         """Get the next event from this process and register as a callback.
                 event = None
                 self.ok = True
                 self._value = e.args[0] if len(e.args) else None
-                self.env.enqueue(DEFAULT_PRIORITY, self)
+                self.env.schedule(self, DEFAULT_PRIORITY)
                 break
             except BaseException as e:
                 # Process has failed.
                 self._value.__cause__ = e
                 if PY2:
                     self._value.__traceback__ = sys.exc_info()[2]
-                self.env.enqueue(DEFAULT_PRIORITY, self)
+                self.env.schedule(self, DEFAULT_PRIORITY)
                 break
 
             # Process returned another event to wait upon.
 
 
 class Scheduler(object):
-    """Schedulers trigger events at a specific points in time."""
+    """Schedulers manage the event queue of an :class:`Environment`.
+
+    They schedule/enqueue new events and pop events from the queue.
+
+    """
     def __init__(self, env):
         self.env = env
         self.eid = count()
         self.queue = []
         self.now = 0
 
-    def enqueue(self, priority, event):
-        heappush(self.queue, (self.now, priority, next(self.eid), event))
-
-    def schedule(self, delay, priority, event):
-        heappush(self.queue, (self.now + delay, priority, next(self.eid), event))
+    def schedule(self, event, priority, delay=0):
+        """Schedule an *event* with a given *priority* and a *delay*."""
+        heappush(self.queue, (self.now + delay, priority, next(self.eid),
+                              event))
 
     def peek(self):
-        """Get the time of the next scheduled event. If there are currently no
-        events scheduled, ``Infinity`` is returned."""
+        """Get the time of the next scheduled event. Return ``Infinity`` if the
+        event queue is empty."""
         try:
             return self.queue[0][0]
         except IndexError:
         """Remove and returns the next event from the queue.
 
         This method advances the simulation time of the environment.
+
         """
         try:
             self.now, _, _, event = heappop(self.queue)
         self.process = types.MethodType(Process, self)
         self.start = types.MethodType(Process, self)
 
-        self.enqueue = self.scheduler.enqueue
         self.schedule = self.scheduler.schedule
         self.fetch = self.scheduler.fetch
 
         # Schedule the event with before all regular timeouts.
         until = env.event()
         until._value = None
-        env.schedule(at - env.now, HIGH_PRIORITY, until)
+        env.schedule(until, HIGH_PRIORITY, at - env.now)
 
     until.callbacks.append(_stop_simulate)