[Twisted-Python] Dealing with an intermittent PB server

Luc Stepniewski luc.stepniewski at adelux.fr
Wed Feb 16 05:02:21 EST 2005


On Wednesday 16 February 2005 04:56, Dave Cook wrote:
> I'm rendering the results of a remote method call:
>
>     def data_tableList(self, ctx, data):
>         ...
>         d =   self.pbClientFactory.login(creds)
>         d.addCallback(lambda object: object.callRemote("foo"))
>         return d
> but seems like a hack.  Also, ideally, I'd like to attempt a
> reconnection to the PB server at this point if it's not running.
> What's the best way to do that?

Maybe it's because the connection succeeds, but *after* that, your server 
crashes or something else, so it's not catched by this code. You should use 
the notifyOnDisconnect() method for this. Here's a snippet of code I use 
regularly to do this:

[...]
def connect(self):
  factory = pb.PBClientFactory()
  reactor.connectTCP(distantServer, distantPort, factory)
  def1 = factory.login(credentials.UsernamePassword(user, password),                          
            client=self)
  def1.addCallbacks(callback=self.connected, errback=self.noLogin)

def noLogin(self, reason):
  reactor.callLater(5, self.connect) # retry after 5 seconds

def connected(self, perspective):
  self.presence = perspective
  perspective.notifyOnDisconnect(self.server_disconnected)

def server_disconnected(self, ref):
  reactor.callLater(5, self.connect) # retry after 5 seconds
[...]


Luc
-- 
Luc Stepniewski <luc.stepniewski at adelux.fr>
Adelux - Securite, Linux  Public key: <http://lstep.free.fr/pubkey.txt>
Key BC0E3C2A fingerprint = A4FA466C68D27E46B427  07D083ED6340BC0E3C2A




More information about the Twisted-Python mailing list