Python Manual Benutzerhandbuch

Seite von 124
Service Lookup
45
class PollingService(netsvc.Service):
def __init__(self,name,period=60):
netsvc.Service.__init__(self)
self._name = name
self._period = period
self.initiateRequests("poll")
def initiateRequests(self,tag):
bindings = self.lookupServiceName(self._name)
for binding in bindings:
service = self.serviceEndPoint(binding)
# presume remote service provides uptime method
id = service.uptime()
self.processResponse(self.handleResult,id)
self.startTimer(self.initiateRequests,self._period,"poll")
def handleResult(self,result):
address = self.currentResponse().sender()
binding = self.lookupServiceAddress(address)
if binding != None:
print binding.agentIdentity(),result
A number of different types of lookup can be made against the service registry. The first two allow you
to lookup all service agents which have a particular service name, or all service agents which are cur-
rently a member of a specific service group. The two member functions corresponding to these lookups
are "
lookupServiceName()
" and "
lookupServiceGroup()
". Both these lookup functions
return a list of service binding objects corresponding to the service agents found. If there are no service
agents matching the search criteria, an empty list is returned.
The third type of lookup is that of looking up a specific service agent using its service address. In this
case you will need to have been able to obtain the service address by some other means first. The mem-
ber function here is "
lookupServiceAddress()
". The result will be the service binding object
corresponding to that service agent, or
None
if the service agent is no longer available.
In order to obtain a list of all services known of by the service registry, the member function "
serv-
iceAgents()
" can be used. This should however be used sparingly because of the overhead which
might be incurred when there are large numbers of services. If possible, subscription against the serv-
ice registry should still be used if it is necessary to track all available services. Overhead can be reduced
by using subscription and caching the results as Python data structures, with Python objects accessing
the cache directly. This avoids the translation from C++ data structures to Python data structures.
Similarly to service agents, a list of all service groups can be obtained by calling the member function
"
serviceGroups()
". If it is necessary to determine which service groups a particular service agent
is a member of, an optional argument can be supplied to "
serviceGroups()
", that argument being
the service address of the service agent of interest.