[Twisted-Python] Passing extra arguments along callbacks

Jonathan Ballet jon at multani.info
Fri Oct 21 20:08:36 MDT 2011


Hi Terry,

On Fri, Oct 21, 2011 at 09:50:56PM -0400, Terry Jones wrote:
> Hi Jonathan
> 
> > def resolve(ip):
> >     ptr = '.'.join(ip.split('.')[::-1]) + '.in-addr.arpa'
> >     return client.lookupPointer(ptr).\
> >             addCallback(lambda x: (ip, x)).\
> >             addErrback(lambda x: (ip, x))
> 
> Your errback isn't raising and isn't returning a Failure, so the processing
> switches back to the callback chain. If you add an errback that's a real
> function and it raises something, things will work. E.g.
> 
> def err(fail, ip):
>     raise Exception(fail, ip)
> 
> return client.lookupPointer(ptr).\
>     addCallback(lambda x: (ip, x)).\
>     addErrback(err, ip)

OK, it works better :)
I read about the behavior of the errback() "switching back" to the
normal flow if it wasn't raising an exception or returning the Failure,
but I wasn't sure how to wire things together. It was really simple
actually!

> > l = [resolve(ip) for ip in IPS]
> > d = DeferredList( l, consumeErrors=True).\
> >         addCallback(display_results).\
> >         addCallback(lambda x: reactor.stop)
> 
> Try reactor.stop()

*slap my head*

Sure...

Thanks for your quick answers!

 Jonathan




More information about the Twisted-Python mailing list