Credit: Simon Foster
You want to implement Python clients and servers for some distributed processing task, without repetitious "boilerplate" code, and with excellent performance and scalability characteristics.
Use the Perspective Broker (PB) subsystem
of the Twisted framework. A PB server just
subclasses the PB's
Root class and
adds remotely callable methods. Here is an example of a server script
which adds just one remotely callable method, named
from twisted.spread import pb from twisted.internet import reactor PORT = 8992 class Ponger(pb.Root): def remote_Pong(self, ball): print 'CATCH', ball, ball += 1 print 'THROW', ball return ball reactor.listenTCP(PORT, pb.BrokerFactory(Ponger( ))) reactor.run( )
We could write an equally trivial script for the client side of the interaction, but let's instead have a rather feature-rich PB client, which deals with important issues often ignored in introductory examples of distributed programming, such as error handling:
from twisted.spread import pb from twisted.internet import reactor import sys PORT = 8992 DELAY = 1 DOG_DELAY = 2 RESTART_DELAY = 5 class Pinger(object): def _ _init_ _(self, host): self.ping = None self.host = host self.ball = 0 self._start( ) def _start(self): print 'Waiting for Server', self.host dfr = pb.getObjectAt(self.host, PORT, 30) dfr.addCallbacks(self._gotRemote, self._remoteFail) def _gotRemote(self, remote): remote.notifyOnDisconnect(self._remoteFail) ...