Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
scheduler Class Reference

Public Member Functions

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

Data Fields

 queue
 
 timefunc
 
 delayfunc
 

Detailed Description

Definition at line 35 of file sched.py.

Constructor & Destructor Documentation

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

Definition at line 36 of file sched.py.

36 
37  def __init__(self, timefunc, delayfunc):
38  """Initialize a new instance, passing the time and delay
39  functions"""
40  self.queue = []
41  self.timefunc = timefunc
42  self.delayfunc = delayfunc

Member Function Documentation

def 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 RuntimeError.

Definition at line 63 of file sched.py.

63 
64  def cancel(self, event):
65  """Remove an event from the queue.
66 
67  This must be presented the ID as returned by enter().
68  If the event is not in the queue, this raises RuntimeError.
69 
70  """
71  self.queue.remove(event)
def empty (   self)
Check whether the queue is empty.

Definition at line 72 of file sched.py.

References mutex.queue, scheduler.queue, and Queue.queue.

72 
73  def empty(self):
74  """Check whether the queue is empty."""
75  return len(self.queue) == 0
def 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 54 of file sched.py.

References scheduler.enterabs(), and scheduler.timefunc.

54 
55  def enter(self, delay, priority, action, argument):
56  """A variant that specifies the time as a relative time.
57 
58  This is actually the more commonly used interface.
59 
60  """
61  time = self.timefunc() + delay
62  return self.enterabs(time, priority, action, argument)
def 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 43 of file sched.py.

References bisect.insort, mutex.queue, scheduler.queue, and Queue.queue.

43 
44  def enterabs(self, time, priority, action, argument):
45  """Enter a new event in the queue at an absolute time.
46 
47  Returns an ID for the event which can be used to remove it,
48  if necessary.
49 
50  """
51  event = time, priority, action, argument
52  bisect.insort(self.queue, event)
53  return event # The ID
def 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 questionably 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 76 of file sched.py.

References scheduler.delayfunc, mutex.queue, scheduler.queue, Queue.queue, and scheduler.timefunc.

76 
77  def run(self):
78  """Execute events until the queue is empty.
79 
80  When there is a positive delay until the first event, the
81  delay function is called and the event is left in the queue;
82  otherwise, the event is removed from the queue and executed
83  (its action function is called, passing it the argument). If
84  the delay function returns prematurely, it is simply
85  restarted.
86 
87  It is legal for both the delay function and the action
88  function to to modify the queue or to raise an exception;
89  exceptions are not caught but the scheduler's state remains
90  well-defined so run() may be called again.
91 
92  A questionably hack is added to allow other threads to run:
93  just after an event is executed, a delay of 0 is executed, to
94  avoid monopolizing the CPU when other threads are also
95  runnable.
96 
97  """
98  q = self.queue
99  while q:
100  time, priority, action, argument = q[0]
101  now = self.timefunc()
102  if now < time:
103  self.delayfunc(time - now)
104  else:
105  del q[0]
106  void = apply(action, argument)
107  self.delayfunc(0) # Let other threads run

Field Documentation

delayfunc

Definition at line 41 of file sched.py.

queue

Definition at line 39 of file sched.py.

timefunc

Definition at line 40 of file sched.py.


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