[Twisted-Python] Interleaving long running loops

Phil Mayers p.mayers at imperial.ac.uk
Wed Nov 2 05:06:45 MDT 2011


On 02/11/11 10:40, Umit Oztosun wrote:

> I have lots of similar loops all over the place and a more generic
> solution would be great. I am also aware of deferToThread but I am
> trying to avoid using threads.

Well, deferToThread gives you all kinds of problems re: talking to the 
reactor from threads, so avoid.

You could try it as a cooperator

from twisted.internet import task

@defer.inlineCallbacks
def operation():
   result = yield somesql()
   # n.b. generator expression!
   work = (process(item) for item in result)
   yield task.coiterate(work)




More information about the Twisted-Python mailing list