[Twisted-Python] reactor.stop() won't, threads and Queue to blame?
Clark C. Evans
cce at clarkevans.com
Sun Oct 24 23:27:15 EDT 2004
""" Sometimes you want to make sure that only one secondary
thread is being used for a sequence of calls. This can
be accomplished with a Queue as proposed by Brett Viren.
"""
from twisted.internet import reactor,defer,threads
from Queue import Queue, Empty
class CommandQueue:
''' Queue up commands for serial calling. One must call the
drain() method to start reading the internal queue. Most likely
one wants to call this in a thread. '''
def __init__(self):
"Create a CommandQueue"
self.queue = Queue()
self.running = False
return
def _cbRunQueue(self, cbval = None):
try:
cd,meth,a,k = self.queue.get(True,1)
d = threads.deferToThread(meth, *a, **k)
d.addBoth(self._cbRunQueue)
d.chainDeferred(cd)
except Empty:
self.running = False
return cbval
def __call__(self,meth,*a,**k):
'''Call meth(*a,**k) when it reaches end of queue. Returns a
Deferred that will pass the return of meth.'''
d = defer.Deferred()
self.queue.put((d,meth,a,k))
if not self.running:
self.running = True
self._cbRunQueue()
return d
def test1():
import time
cq = CommandQueue()
def shutdown(x=None):
print "Stopping reactory"
reactor.stop()
print "reactor.stop()'ed"
def burp(x):
for n in range(0,x):
time.sleep(1)
print x,n
return x
def chirp(x):
print "okay:",x
return None
def ouch(x):
print "bad:",x
return x
last = 3
for n in range(0,last):
print "dispatching",n
d = cq(burp,n).addCallbacks(chirp,ouch)
if last-n == 1:
d.addCallbacks(shutdown,ouch)
if __name__ == '__main__':
print "running test1"
test1()
print "end test1"
reactor.run()
print "reactor exitted"
--
Clark C. Evans Prometheus Research, LLC.
http://www.prometheusresearch.com/
o office: +1.203.777.2550
~/ , mobile: +1.203.444.0557
//
(( Prometheus Research: Transforming Data Into Knowledge
\\ ,
\/ - Research Exchange Database
/\ - Survey & Assessment Technologies
` \ - Software Tools for Researchers
~ *
More information about the Twisted-Python
mailing list