def iterateInReactor(i):
 from twisted.internet import reactor
 d = defer.Deferred()
 def go(last):
  try:
   r = next(i)
  except StopIteration:
   d.callback(last)
  except:
   d.errback()
  else:
   if isinstance(r, defer.Deferred):
    r.addCallback(go)
   else:
    reactor.callLater(0, go, r)
 go(None)
 return d

def iterateInReactor ( i ) : from twisted . internet import reactor d = defer . Deferred ( ) def go ( last ) : try : r = next ( i ) except StopIteration : d . callback ( last ) except : d . errback ( ) else : if isinstance ( r , defer . Deferred ) : r . addCallback ( go ) else : reactor . callLater ( 0 , go , r ) go ( None ) return d
consume an interator at most a single iteration per reactor iteration .