Python 7.0pl5 Electronic Accessory User Manual


 
Scheduling a Job
27
All that is occuring here is that when the "cancel()" member function is called, a flag is set. When
the job is executed it will note that the flag is set and will not execute the callback function. If the call-
back function is a member function of a class, it is important to ensure that any reference to the instance
of the Job class is destroyed when no longer required. If this is not done and the reference is a member
variable of the same class the callback function is a member of, a circular reference will exist and that
instance of the class will not be able to be destroyed.
Any arguments to be passed to the callback function would by default be supplied when the instance
of the Job class is created. If it is necessary to generate an instance of the Job class such that it can
be passed to another part of the program, but the arguments to the callback function are not known at
that time, it is instead possible to supply the arguments at the time the job is scheduled. This is done
by using the "schedule()" member function of the Job class rather than that of the dispatcher. Any
arguments supplied in this way will override those provided when the instance of the Job class is cre-
ated.
job = None
def callback1(message):
print message
job.schedule(netsvc.STANDARD_JOB,("override",))
def callback2(message)
print message
job = netsvc.Job(callback1,("default",))
job.schedule(netsvc.STANDARD_JOB)
job = netsvc.Job(callback2)
This would allow for instance a class which accepts callback registrations to return a reference to a
Job class which will later be used to schedule the callback with an as yet undetermined set of argu-
ments. The client who registered the callback could however cancel execution of the callback before
it is called.
Once "cancel()" has been called on an instance of a Job class, whether or not it has already been
scheduled, the callback function will never be executed. To reset the flag which makes the callback
function runnable, the "reset()" member function should be called. To determine if the an instance
of the Job class is still in a runnable state, a truth test can be performed on it.
if job:
# job wasn’t cancelled
job.schedule(netsvc.STANDARD_JOB)
else:
# job was cancelled
pass
If you wish to use the Job class separate to the dispatcher, you can trigger execution of the callback
function by calling the "execute()" member function. If any arguments are supplied to the "exe-