[Twisted-Python] Handling PBConnectionLost errors

Phil Mayers p.mayers at imperial.ac.uk
Wed Jul 25 11:09:47 EDT 2007

On Wed, 2007-07-25 at 10:38 -0400, Daniel Miller wrote:
> Is this such a stupid question that it doesn't even warrant a response?

Not really, but it's a more complex question than it first seems, and
you're trying very hard to reproduce things that already exist.

First question: why is your PB server allocating "request IDs" and
storing hashes of them. You could just return a pb.Referenceable to the
client, backed by a normal python object (with a normal object
lifecycle) on the server.

Regarding the retry mechanism: It looks to me like you're treating PB
like an RPC mechanism, and finding that the nature of networks (they
fail, unpredictably) is tripping you up. Try thinking of it in a more
message-oriented way.

I would implement it something like this, using the python2.5 yield and
inlineCallbacks functionality:

class MyResources:
  def render_POST(self, request):
    d = self.doOnlyOnce(request)
    d.addCallbacks(self.done, self.failed, (request,), (request,))
    return server.NOT_DONE_YET

  def done(self, data, request):
  def failed(self, f, request):
    request.write("An error occured")

  def doOnlyOnce(self, request):
    pbroot = yield self.pbclifactory.getRootObject()
    theobject = yield pbroot.callRemote('makeRequest')
    data = yield theobject.callRemote('someMethod', request.args)
    # some local code here, then...
    # ok, done, tell the remote object to "commit" (finalise, delete)
    yield theobject.callRemote('commit')

Your example is a bit theoretical, so it's difficult to see if this
would work for you.

More information about the Twisted-Python mailing list