Python 7.0pl5 Electronic Accessory User Manual


 
Program Shutdown
33
ating system is being shutdown. Other uses for program signals are to force an application to reread a
configuration file.
These three cases are typically indicated by the program signals SIGINT, SIGTERM and SIGHUP.A
robust application should at least catch the first two of these signals and cause the program to shutdown
gracefully. This may entail ensuring that any data is written out to files, removal of file locks, closing
off of database connections etc.
To subscribe to a signal, the member function "subscribeSignal()" should be used. The first ar-
gument should be a callback function to be called when a particular signal occurs and the second ar-
gument the particular signal of interest. A particular agent may only supply one callback for any
particular signal, but different agents may subscribe to the same signal with both being notified when
it occurs. Although an interest in such a signal is usually persistent, it is possible to unsubscribe from
a particular signal using the member function "unsubscribeSignal()" and unsubscribe from all
signals using "unsubscribeAllSignals()".
class Agent(netsvc.Agent):
def __init__(self):
netsvc.Agent.__init__(self)
self.subscribeSignal(self.signal,signal.SIGINT)
self.subscribeSignal(self.signal,signal.SIGTERM)
def signal(self,signum):
self.scheduleAction(self.stop,netsvc.PRIORITY_JOB)
def stop(self):
netsvc.Dispatcher().stop()
In practice, only one of the agents subscribed to SIGINT and SIGTERM should actually shutdown the
dispatcher. This agent should however, not shutdown the dispatcher immediately as other agents may
not yet have received their notification that the signal occurred. The agent should instead schedule a
priority job to actually stop the dispatcher. This priority job will only be executed after all outstanding
signal notifications have been delivered.
Program Shutdown
Subscription to a program signal provides a means of immediately shutting down an application when
caused to do so by an external signal. What program signals don’t do however, is provide a means of
initiating a graceful shutdown of an application from within the application itself. An application could
send itself a signal, however, this isn’t necessarily practical.
A further problem is that in an event driven system, it may not always be possible to perform every-
thing that is required in a single callback function. What is instead needed is the ability to run the ap-
plication for a further finite amount of time so that any outstanding operations can be finalised first. At
the end of that time, then the application can be stopped.
To support this slightly more orderly mechanism for program shutdown, the member function
"scheduleShutdown()" is provided. When an agent wishes to force the program to shutdown it