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

Tommi Virtanen tv at twistedmatrix.com
Fri Aug 1 04:15:33 EDT 2003

On Thu, Jul 31, 2003 at 02:20:24PM -0600, Justin Johnson wrote:
>      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.

	Yes. I'd make that prettier with

    def onSuccess(results, item):
        d = item.doSomethingElseThatReturnsDeferred()
        return d
    for item in list:
	d.addCallback(onSuccess, item)

> > 	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 original code had

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

	Which ends up adding the _same_ d len(list) times to deferreds.

:(){ :|:&};:

More information about the Twisted-Python mailing list