[Twisted-Python] Defgen explanation
Paolino
paolo.veronelli at gmail.com
Sat Jul 23 09:52:17 MDT 2005
Hello all,
I have the next running script which cause me some questions.
I thought deferredGenerators tech could be easily reconverted to
Deferreds ,but I'm having no success in doing it on the piece of code in
Indexer.do method.
Particularly if I substitute the generator with
DeferredList([indexing() for indexing in
indexings]).addCallback(self.do) I fall in recursion.But the
rescheduling has to happen really at the end of the indexing.
I think I miss something :(
Thanks for help
from twisted.internet import reactor
from twisted.internet.defer import waitForDeferred as
wait,deferredGenerator as async,Deferred,DeferredList
import time
def deferredReady(value=False,later=0):
deferred=Deferred()
reactor.callLater(later,deferred.callback,value)
return deferred
coop=lambda:wait(deferredReady())
class Indexer(object):
def do(self,*_):
indexings=self.indexings
self.indexings=[] # a new queue
##### how to substitute this with non-generator deferring ?
def _():
yield wait(DeferredList([indexing() for indexing in indexings]))
deferredReady().addCallback(self.do) # rescheduling
async(_)()
####### end of substitution
def __init__(self):
self.indexings=[] # a queue
self.do() # first scheduling
def index(self,uri):# the interface for triggers
def _():
print 'indexing ',uri
for i in range(10000): #adjust this higher to see the queue
growing if your machine is faster
yield coop()
print 'indexed',uri
self.indexings.append(async(_))
print 'Queued indexings',len(self.indexings)
indexer=Indexer()
from twisted.internet import task
task.LoopingCall(lambda :indexer.index('ciao')).start(3)
task.LoopingCall(lambda :indexer.index('mamma')).start(4)
task.LoopingCall(lambda :indexer.index('papa')).start(5)
reactor.run()
More information about the Twisted-Python
mailing list