[Twisted-Python] looping in a non-blocking way

Tommi Virtanen tv at twistedmatrix.com
Thu Mar 24 14:20:54 EST 2005


Eugene Coetzee wrote:
> def importFromText(self,data):
>               datalines=data.split("\r\n")
>                for line in datalines:
>                        arrin=[]
>                        arrin.insert(0,line[0:10])
>                        etc....


from twisted.internet import reactor

CHUNKSIZE = 100
def _parseSome(lines):
     for i in xrange(CHUNKSIZE):
         try:
             line = lines.pop(0)
         except IndexError:
             break
         # parse line here, maybe mutate a data
         # structured passed as an argument
     if lines:
         reactor.callLater(0, _parseSome, lines)
     else:
         # all done, probably should notify someone

def importFromText(self, data):
     datalines=data.split("\r\n")
     _parseSome(datalines)




More information about the Twisted-Python mailing list