[Twisted-Python] subclassing and mixing protocol.ReconnectingClientFactory and pb.PBClientFactory

Fabrizio Mancini mr.file at gmail.com
Thu Apr 30 17:03:06 EDT 2009

i've implemented a pb client by writing a class which inherits from
object and a class which inherits from ReconnectingClientFactory and
Till now it's all ok, the client detects the disconnection and try to
reconnect with an exponential delay if the connection is not
immediatly reestabilished.
The calls to the callRemote("get_item") method are repetitive, but for
sake of simplicity i've cut off the code that controls if there is
already another call running.
The problem:
i'd like to manage something like the clientConnectionMade method when
the client reconnect (thanks to the ReconnectingClientFactory) after a
disconnection and launch again the MypbClient.connect() method as i do
in the "if __name__ " statement, but i can't guess how to do it.
This is the code i'm using.
Thanks in advanche, any help appreciated, and hope to have been clear enough

class MypbClient(object):

   def __init__(self):
       self.clientfactory = MypbClientFactory()
       self._scheduledCall] = task.LoopingCall(self.get_item)
       self._scheduledCall].start(10, False)

   def connect(self):
       reactor.connectTCP(server_ip, server_port, self.clientfactory)
       self.rootObj = self.clientfactory.getRootObject()

   def initiateChain(self, broker):
       self.broker = broker
       self.d = self.broker.callRemote("get_item")

   def get_item(self, result):
       self.d = self.broker.callRemote("get_item")

   def got_item(self, record):
       self.op = doSomethingWith(record) #returns a deferred

class MypbClientFactory(pb.PBClientFactory, protocol.ReconnectingClientFactory):
   def __init__(self):

   def clientConnectionMade(self, broker):
       pb.PBClientFactory.clientConnectionMade(self, broker)

   def clientConnectionFailed(self, connector, reason):
       pb.PBClientFactory.clientConnectionFailed(self, connector, reason)
       if self.continueTrying:
           self.connector = connector

   def clientConnectionLost(self, connector, reason, reconnecting=1):
       pb.PBClientFactory.clientConnectionLost(self, connector,
reason, reconnecting=reconnecting)
connector, reason)

if __name__ == "__main__":
   dpc = MypbClient()

More information about the Twisted-Python mailing list