Service Availability

er endpoint and a new client connects, a subscriber to that service in the client will be notified that the service is available. Similarly, any service within a client will become visible from the server as well as other connected clients.

Although the service is located in a separate process, the same service registry interface is used to sub- scribe to the presence of the service. Subscription to reports produced by the service and the issuing of requests against that service are also mediated through the same interface as previously described. The only exception to this is that the LocalService proxy class cannot be used to communicate with any service in a remote process, it being restricted to services implemented using Python which appear in the same process.

Except for the LocalService proxy class, that there is no distinction in the interface to communi- cate between services whether they be in the same or a remote process, means that it is a simple matter to split an application across multiple processes. If a distinct message exchange server process is used, all that is required is that each process embed a message exchange client and connect to the message exchange server.

As an example, a process supporting a service which publishes periodic reports would be written as follows.

class Publisher(netsvc.Service): def __init__(self):

netsvc.Service.__init__("publisher") self.publishReport("system.ctime",netsvc.DateTime(),-1) self.startTimer(self.timeout,10,"heartbeat")

def timeout(self,tag): self.publishReport("system.time",netsvc.DateTime()) self.startTimer(self.timeout,10,"heartbeat")

dispatcher = netsvc.Dispatcher() dispatcher.monitor(signal.SIGINT)

exchange = netsvc.Exchange(netsvc.EXCHANGE_CLIENT) exchange.connect("localhost",11111,5) dispatcher.run()

The process containing the corresponding subscriber to this service would then be written as follows.

class Subscriber(netsvc.Service): def __init__(self):

netsvc.Service.__init__(self) self.monitorReports(self.report,"publisher","system.*")

def report(self,service,subject,content): name = service.serviceName() identity = service.agentIdentity() publisher = "(%s/%s)" % (`name`,identity) if subject == "system.ctime":

now = str(netsvc.DateTime())

print "%s became available at %s" % (publisher,now)

73

Page 73
Image 73
Python 7.0pl5, Python Manual manual Service Availability