Service Requests

raised, you should avoid an except clause which catches all exceptions in any code which encloses code which might call "abortResponse()". Alternatively, you should explicitly pass on excep- tions of type ServiceFailure.

try: self.execute(...)

except netsvc.ServiceFailure: raise

except: self.abortResponse(...)

If many of the public methods of a service generate the same type of exceptions, rather than provide code to catch the exceptions in every method, it is possible to override the member function "exe- cuteMethod()". This member function is called by the service agent framework to call the actual member function referred to by a service request. It is important to preserve the existing functionality of this method otherwise service requests will not execute correctly.

class Database(netsvc.Service):

# ...

def executeMethod(self,name,method,params): try:

return netsvc.Service.executeMethod(self,name,method,params) except MySQLdb.ProgrammingError,exception:

details = netsvc.exceptionDetails() self.abortResponse(1,"Programming Error","db",details)

except MySQLdb.Error,(error,description): self.abortResponse(error,description,"mysql")

The member function "executeMethod()" might also be overridden if you want to track what re- quests are being made against a service. The arguments to the member function are the name of the method, the actual member function and the parameters to be supplied when the member function is called.

Delaying a Response

In a distributed application, it is sometimes the case that when a method is called it doesn’t have the information necessary to generate an immediate response. This may be the case where it needs to ini- tiate its own service requests to accumulate the data needed to generate the result. Because the service agent framework is based on an event driven system, it is not possible for the method to simply block waiting for its own data. This is because the method must return before anything else can execute.

To deal with this, the member functions "suspendResponse()" and "resumeResponse()" are provided. If the "suspendResponse()" member function is called, it will raise an exception which will be caught by the service agent framework. The name of this exception is DelayedResponse and lets the service agent framework know that a response will be sent at a later time.

66

Page 66
Image 66
Python Python Manual, 7.0pl5 manual Delaying a Response