[Twisted-Python] Please help with Deferreds

Andrew Bennetts andrew-twisted at puzzling.org
Mon Aug 26 20:24:29 EDT 2002


On Mon, Aug 26, 2002 at 08:50:34PM +0300, Tommi Virtanen wrote:
> On Mon, Aug 26, 2002 at 11:43:46AM +1000, Andrew Bennetts wrote:
> > > #!/usr/bin/python
> > > import sys
> > > from twisted.internet import defer, reactor
> > > d=defer.Deferred()
> > > d.callback(0)
> > > d2=defer.Deferred()
> > > d.addCallback(lambda x,d2=d2: d2)
> > 
> > So you've effectively done:
> > 
> > d = defer.Deferred()
> > d2 = defer.Deferred()
> > d.callback(d2)
> 
> 	Not really. You'll notice that Deferred addCallbacks
> 	handlers returning Deferreds is handled differently
> 	from d.callback(Deferred()); the former gets special
> 	treatment, the latter doesn't.
> 
> 	Your version never outputs "1", it outputs repr(d2)
> 	for the callLater version and repr(d) for the immediate
> 	one.
> 
> 	My version outputs "1" for the immediate case and
> 	repr(d2) for the callLater version.

Hmm.  Good point.

Deferreds are more complicated that I thought :(

I think this implicit chaining behaviour needs to become more
prominently discussed in the documentation, because that's what confused
me...  Probably what needs updating in doc/howto/defer is the diagram
explaining the chain of callback processing (or the text next to it),
which never at any point hints at this behaviour (that's described
later).  I'll probably checkin a doc fix along the lines of "Note that
this behaves differently if a callback returns a Deferred; see 'Chaining
Deferreds' for more details".

-Andrew.





More information about the Twisted-Python mailing list