[Twisted-Python] invoking Deferred synchronously

Laurie Harper laurie at holoweb.net
Tue Aug 21 03:09:41 MDT 2007


I'm sure this is trivial enough, but it has me scratching my head... I 
need to call a method that returns a Deferred, then block until the 
deferred completes (or errback's) and return the final result of the 
deferred call.

Specifically, something like:

     r = None, is_ok = None
     def ok(p): r, is_ok = p, True
     def fail(e): r, is_ok = e, False

     cf = pb.PBClientFactory()
     reactor.connectTCP(host, port, cf)
     d = cf.login(creds, client=self).addCallbacks(ok, fail)

     # BLOCK: wait until d has called 'ok' of 'fail'

     if is_ok:
         return r
     else:
         raise r

If it helps, I'm trying to create a web application (using Nevow) that 
connects to another Twister service through the Persistence Broker. I'm 
trying to implement authentication, and so need a way to wait for the PB 
connection to succeed/fail so I can send the appropriate response to the 
HTTP request.

Any pointers would be very much appreciated! :-)

L.





More information about the Twisted-Python mailing list