[Twisted-Python] Design Pattern for Iterating Through Lists
Ken Kinder
ken at kenkinder.com
Mon Mar 14 11:37:15 EST 2005
I've come almost to the point of making a template for this kind of
operation:
d = Deferred()
d.callback(0)
results = []
def doItem(void, eggs):
return whatever(eggs)
def processResult(result):
results.append(result)
for spam in list:
d.addCallback(doItem, spam['eggs'])
d.addCallback(processResult)
d.addCallback(lambda _: results)
return d
The reason I'm calling back on doItem is that lambda will evaluate its
variables one for the iteration, causing only the first evaluation of
spam['eggs'] to be passed for each item in the list. Is there a more
readable/efficient way of doing this? Note that I'm not using a
DeferredList because I want everything processed serially, not in parallel.
-Ken
More information about the Twisted-Python
mailing list