Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Attributes
sched.scheduler Class Reference

List of all members.

Public Member Functions

def __init__
def enterabs
def enter
def cancel
def empty
def run
def queue

Public Attributes

 timefunc
 delayfunc

Private Attributes

 _queue

Detailed Description

Definition at line 44 of file sched.py.


Constructor & Destructor Documentation

def sched.scheduler.__init__ (   self,
  timefunc,
  delayfunc 
)
Initialize a new instance, passing the time and delay
functions

Definition at line 45 of file sched.py.

00045 
00046     def __init__(self, timefunc, delayfunc):
00047         """Initialize a new instance, passing the time and delay
00048         functions"""
00049         self._queue = []
00050         self.timefunc = timefunc
00051         self.delayfunc = delayfunc

Here is the caller graph for this function:


Member Function Documentation

def sched.scheduler.cancel (   self,
  event 
)
Remove an event from the queue.

This must be presented the ID as returned by enter().
If the event is not in the queue, this raises ValueError.

Definition at line 72 of file sched.py.

00072 
00073     def cancel(self, event):
00074         """Remove an event from the queue.
00075 
00076         This must be presented the ID as returned by enter().
00077         If the event is not in the queue, this raises ValueError.
00078 
00079         """
00080         self._queue.remove(event)
00081         heapq.heapify(self._queue)

Here is the call graph for this function:

Here is the caller graph for this function:

def sched.scheduler.empty (   self)
Check whether the queue is empty.

Definition at line 82 of file sched.py.

00082 
00083     def empty(self):
00084         """Check whether the queue is empty."""
00085         return not self._queue

def sched.scheduler.enter (   self,
  delay,
  priority,
  action,
  argument 
)
A variant that specifies the time as a relative time.

This is actually the more commonly used interface.

Definition at line 63 of file sched.py.

00063 
00064     def enter(self, delay, priority, action, argument):
00065         """A variant that specifies the time as a relative time.
00066 
00067         This is actually the more commonly used interface.
00068 
00069         """
00070         time = self.timefunc() + delay
00071         return self.enterabs(time, priority, action, argument)

Here is the call graph for this function:

Here is the caller graph for this function:

def sched.scheduler.enterabs (   self,
  time,
  priority,
  action,
  argument 
)
Enter a new event in the queue at an absolute time.

Returns an ID for the event which can be used to remove it,
if necessary.

Definition at line 52 of file sched.py.

00052 
00053     def enterabs(self, time, priority, action, argument):
00054         """Enter a new event in the queue at an absolute time.
00055 
00056         Returns an ID for the event which can be used to remove it,
00057         if necessary.
00058 
00059         """
00060         event = Event(time, priority, action, argument)
00061         heapq.heappush(self._queue, event)
00062         return event # The ID

Here is the call graph for this function:

Here is the caller graph for this function:

def sched.scheduler.queue (   self)
An ordered list of upcoming events.

Events are named tuples with fields for:
    time, priority, action, arguments

Definition at line 129 of file sched.py.

00129 
00130     def queue(self):
00131         """An ordered list of upcoming events.
00132 
00133         Events are named tuples with fields for:
00134             time, priority, action, arguments
00135 
00136         """
00137         # Use heapq to sort the queue rather than using 'sorted(self._queue)'.
00138         # With heapq, two events scheduled at the same time will show in
00139         # the actual order they would be retrieved.
00140         events = self._queue[:]
00141         return map(heapq.heappop, [events]*len(events))
def sched.scheduler.run (   self)
Execute events until the queue is empty.

When there is a positive delay until the first event, the
delay function is called and the event is left in the queue;
otherwise, the event is removed from the queue and executed
(its action function is called, passing it the argument).  If
the delay function returns prematurely, it is simply
restarted.

It is legal for both the delay function and the action
function to to modify the queue or to raise an exception;
exceptions are not caught but the scheduler's state remains
well-defined so run() may be called again.

A questionable hack is added to allow other threads to run:
just after an event is executed, a delay of 0 is executed, to
avoid monopolizing the CPU when other threads are also
runnable.

Definition at line 86 of file sched.py.

00086 
00087     def run(self):
00088         """Execute events until the queue is empty.
00089 
00090         When there is a positive delay until the first event, the
00091         delay function is called and the event is left in the queue;
00092         otherwise, the event is removed from the queue and executed
00093         (its action function is called, passing it the argument).  If
00094         the delay function returns prematurely, it is simply
00095         restarted.
00096 
00097         It is legal for both the delay function and the action
00098         function to to modify the queue or to raise an exception;
00099         exceptions are not caught but the scheduler's state remains
00100         well-defined so run() may be called again.
00101 
00102         A questionable hack is added to allow other threads to run:
00103         just after an event is executed, a delay of 0 is executed, to
00104         avoid monopolizing the CPU when other threads are also
00105         runnable.
00106 
00107         """
00108         # localize variable access to minimize overhead
00109         # and to improve thread safety
00110         q = self._queue
00111         delayfunc = self.delayfunc
00112         timefunc = self.timefunc
00113         pop = heapq.heappop
00114         while q:
00115             time, priority, action, argument = checked_event = q[0]
00116             now = timefunc()
00117             if now < time:
00118                 delayfunc(time - now)
00119             else:
00120                 event = pop(q)
00121                 # Verify that the event was not removed or altered
00122                 # by another thread after we last looked at q[0].
00123                 if event is checked_event:
00124                     action(*argument)
00125                     delayfunc(0)   # Let other threads run
00126                 else:
00127                     heapq.heappush(q, event)

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 48 of file sched.py.

Definition at line 50 of file sched.py.

Definition at line 49 of file sched.py.


The documentation for this class was generated from the following file: