[Twisted-Python] Callbacks, Looping, and Variable Binding

Justin Johnson justinjohnson at fastmail.fm
Thu Jul 31 14:20:24 MDT 2003


On Thu, 31 Jul 2003 22:50:04 +0300, "Tommi Virtanen"
<tv at twistedmatrix.com> said:
> On Thu, Jul 31, 2003 at 11:13:28AM -0600, Justin Johnson wrote:
> > When I run the following code, everytime the callbacks to "onSuccess" get
> > called, "item" is bound to the last element in "list".  Obviously what I
> > want is to have "item" bound to the value it had when I added the
> > callback.  How does one handle looping constructs like this in Twisted?
> 
> 	Then don't inherit the namespace of someFunc. You could even
> 	move onSuccess outside of someFunc, to make sure you don't do
> 	that by accident.

My problem (besides just having a lot to learn) was that I was writing
the callbacks to use variables that were defined in the loop, thinking I
had no other option besides using results passed by previous callbacks. 
The results that were returned from doSomethingThatReturnsDeferred (and
thus were arguments to the next callback) were needed (not right away,
but I wanted the results to continue being passed down the callback
chain), and I wasn't aware of any other way to get "item" passed to my
callback.  However, I tried using callbackArgs to pass "item" in a tuple,
like this...

     for item in list:
         d.addCallbacks(onSuccess, log.err, callbackArgs=(item,))
         deferreds.append(d)

and then defining onSuccess like...

    def onSuccess(results, args):
        item = args[0]
        d = item.doSomethingElseThatReturnsDeferred()
        return d

Is this an acceptable way to accomplish this?  It is working in my code
but I'd like to do things the right way.

> 
> 	Besides, you are adding the outer d to deferreds once per
> 	iteration. That's broken.

I'm not sure what you mean here.  I was adding my deferred to a list and
it seemed to be doing what I wanted (at least I think it was).

Your help is appreciated.
-Justin




More information about the Twisted-Python mailing list