Python Manual User Manual

Page of 124
Event Framework
28
cute()
" member function, these will override any which may have been supplied when that instance
of the
Job
class was created.
Real Time Events
The Python interface provides the ability to register interest in a number of real time events. These are
program shutdown, one off alarms or actions, recurring actions, timers, signals and data activity on
sockets. That an event of interest has occurred is notified by execution of a callback supplied at the
time that interest in an event is registered.
In the C++ implementation, the methods for expressing interest in a specific type of event were spread
across numerous classes. In the Python interface, all functions for registration of interest in events are
contained within the
Agent
base class. Any object interested in receiving notification of an event oc-
curring is expected to derive from the
Agent
class.
The simplest type of notification isn’t really a real time event at all, but a variation on the concept of
scheduling a job with the dispatcher. Instead of calling the "
schedule()
" member function of the
dispatcher, the "
scheduleAction()
" member function of the
Agent
base class is called.
The major difference between using "
scheduleAction()
" and "
schedule()
" is that when us-
ing "
scheduleAction()
" you can optionally supply an additional string argument to be used as
an identifier for that job. This identifier can be used to cancel the job before it actually gets executed
by calling "
cancelAction()
". If the callback funcion accepts a single argument, the identifier will
also be passed to the callback function as argument. The identifier can thus be used to distinguish be-
tween different jobs calling the same callback function. If an identifier is not explicitly provided, a
unique internal identifier will be created. Whether or not the identifier is set explicitly or created inter-
nally, the identifier used is returned as the result of the "
scheduleAction()
" method.
class Object(netsvc.Agent):
def __init__(self):
self.scheduleAction(self.callback1,netsvc.PRIORITY_JOB)
def callback1(self):
self.scheduleAction(self.callback2,netsvc.IDLE_JOB,"hi")
self.scheduleAction(self.callback2,netsvc.IDLE_JOB,"bye")
def callback2(self,name):
print name
if name == "hi":
self.cancelAction("bye")
dispatcher = netsvc.Dispatcher()
object = Object()
dispatcher.run()
When using the
Agent
class, you still need to run the dispatcher. You do not need to schedule any
jobs directly with the dispactcher, but any initial agents need to be created prior to the dispatcher being
run. Note that in scheduling a job with a particular identifier, any job already scheduled with that agent