[Twisted-Python] reactor.stop() won't, threads and Queue to blame?

Clark C. Evans cce at clarkevans.com
Sun Oct 24 22:59:09 EDT 2004


For starters, I'd keep the 'Queue' in the main thread, and
use callInThread to dispatch the function /w arguments.   Use
a d.callBoth (aka finally) to pop the next item from the queue
and then do a callInThread for it.

However, if you want to keep the Queue in the secondary thread,
you have one problem that is obvious to me:

On Sun, Oct 24, 2004 at 08:39:07PM -0400, Brett Viren wrote:
| class CommandQueue:
|     def drain(self):
|         'Drain the command queue until CommandQueue.stop is True'
|         while not self.stop:
|             try:
|                 d,meth,a,k = self.queue.get(True,1)
|             except Empty:
|                 print "  queue empty"
|                 continue
|             print "calling %s(%s,%s)"%(meth.__name__,str(a),str(k))
|             d.callback(meth(*a,**k))
|             print "callback done"
|         print "drain closing"
|         return 0
| def test1():
|     import time
|     cq = CommandQueue()
|     reactor.callInThread(cq.drain)

You seem to be doing d.callback in the secondary thread, rather than
in the primary thread.  This could be causing some of the problems
you are experiencing.   It's not customary to use deferreds in any
other but the main thread.

>From a framework perspective, perhaps callback() should raise an 
error if it is called from anything other than the main thread?
Or perhaps even I'm not getting it.



More information about the Twisted-Python mailing list