Python Manual Benutzerhandbuch

Seite von 124
Service Requests
64
self.processFailure(self.uptimeFailure,id,60)
def uptimeResponse(self,result):
print result
def uptimeFailure(self,id,error,description,origin,details):
if origin == "netsvc" and error == netsvc.CLIENT_REQUEST_TIMEOUT:
# timeout occurred
When a timeout occurs, it will be notified as a request failure. The timeout should be the maximum
number of seconds to wait. The callback will be automatically deregistered and if the response did sub-
sequently arrive it would be ignored. If you wanted a timeout to occur but didn’t want the callback to
be deregistered, you would need to create your own timer. If that timer uses the conversation id corre-
sponding to the request as the timer name, the timer will be automatically stopped if a response does
actually arrive. You should not use the conversation id to set up a timer if you have already defined a
timeout when calling the member function "
processFailure()
" as internally it will use the con-
versation id for its own timer.
Servicing a Request
When you send a request, if the remote service agent is implemented using the Python interface, not
just any member function of the service can be called. In order that a member function of a service can
be called, the service agent must have exported it as a public method. This is done by calling the mem-
ber function "
exportMethod()
" and it would normally be done from within the constructor of the
service agent.
class PagingService(netsvc.Service):
def __init__(self,name="SMS"):
netsvc.Service.__init__(self,name)
self.exportMethod(self.time)
self.exportMethod(self.uptime)
self.exportMethod(self.send)
def time(self):
return netsvc.DateTime()
def uptime(self):
# ...
def send(self,number,message):
# ...
By default the method name associated with the member function will be its actual name. If you wish
to export a member function under a different method name, the method name can be supplied as an
extra argument to the "
exportMethod()
" member function.
class PagingService(netsvc.Service):
def __init__(self,name="SMS"):
netsvc.Service.__init__(self,name)
self.exportMethod(self.sendMessage,"send")
def sendMessage(self,number,message):
# ...