Python 7.0pl5 Electronic Accessory User Manual


 
Program Shutdown
35
itly provide the time delay as an argument. If this is done, the argument should express the number of
full or partial seconds as a floating point value.
Using a time delay is a useful starting point, as it provides a means of defining an upper bound on the
amount of time you wish to allow the system to run before it is stopped. Having a small delay and en-
suring everything is done in that time is preferable, as in certain circumstances such as the operating
system sending a SIGTERM to an application on system shutdown, the operating system will usually
forcibly shutdown your application using SIGKILL after 5 seconds if it doesn’t do so of its own ac-
cord.
Although getting away from the goal of having only one mechanism for shutting down a program, in
this circumstance, it may still be preferable to separately identify a SIGTERM signal and deal with it
differently. Here you might only do anything that is absolutely essential and stop the process immedi-
ately. What is the best approach will depend on the specific application in question.
If the problem of a SIGTERM signal is ignored, a further mechanism for delaying actual shutdown of
a process is also provided. If upon receiving notification of a pending shutdown, an agent knows it
needs to wait for some event to occur first, it can call the "suspendShutdown()" member function.
If this is done, although the shutdown delay may expire, actual program shutdown will not occur until
a corresponding call to the "resumeShutdown()" member function. If more than one agent calls
"suspendShutdown()", actual shutdown will not occur until "resumeShutdown()" has been
called a matching number of times.
Although it is possible to suspend the shutdown process in this way, it is not possible to cancel it com-
pletely. But then, if an agent doesn’t call "resumeShutdown()" at some point it would never actu-
ally occur. This wouldn’t be very useful however, as more than likely parts of the application may have
placed themselves into a dormant state.
Finally, as scheduling program shutdown upon a signal occurring would be done in practically all pro-
grams, support for this has been factored into the actual dispatcher. Thus, instead of dedicating a spe-
cific agent to catch any signals, the main program file can contain:
dispatcher = netsvc.Dispatcher()
dispatcher.monitor(signal.SIGINT)
dispatcher.monitor(signal.SIGTERM)
If this interface is used however, the only means of overriding the delay between shutdown being
scheduled and actual shutdown is by the OTCLIB_SHUTDOWNDELAY environment variable.
The dispatcher also provides the member function "shutdown()". This behaves much the same as
the "scheduleShutdown()" member function of the Agent class. The presence of the "shut-
down()" member function in the dispatcher, allows code which is distinct from an agent to also
schedule a program shutdown.