[Twisted-Python] threadedselectreactor - shutting down the foreign event loop

Toby Dickenson tdickenson at devmail.geminidataloggers.co.uk
Mon Jul 18 05:54:57 EDT 2005


Im looking at moving my PyQt app to threadedselectreactor. Previously I was 
keeping twisted and Qt in seperate threads. Avoiding the need to cross that 
thread boundary has been a great simplification, but Im having some problems 
relating to the shutdown of my foreign event loop.

The examples such as 
http://svn.twistedmatrix.com/cvs/trunk/doc/core/examples/threadedselect/pygamedemo.py?view=auto&rev=13596
handle this by:
a. Calling reactor.stop() when there is a GUI shutdown request, 
b. Using reactor.addSystemEventTrigger to shut down the foreign event loop 
after reactor shutdown.

That approach seems untenable for me, for several reasons:
1. Too invasive. I dont want to pollute Qt code with Twisted details.
2. If my app is runing as a COM server then Pythoncom handles the main loop, 
not Qt. More pollution.
3. The reactor doesnt get shutdown if the foreign event loop shuts down for 
some other reason. This leads to deadlocks when the threading module tries to 
join the threads managed by the threadpool module, because the threadpool 
does know to close those threads.

Im having moderate success with the following alternative structure which 
closes the reactor *after* whatever foreign event loop has exited, but it 
feels like I am relying on undocumented reactor implementation accidents 
here:

from twisted.internet.threadedselectreactor import install as twisted_install
reactor = twisted_install()
reactor.interleave(trigger)
try:
        my_event_loop_in_here()
finally:
	# Request that the reactor stops itself. Internally the
        # reactor uses self.callLater, so shutdown is not complete
        # when this method returns.
        reactor.stop()
        # One call to reactor.iterate is sufficient to complete
        # All reactor shutdown events. 
        reactor.iterate()
        # Reactor is now fully shutdown

Is there a better approach?

Should something like my finally block be included as a method of 
threadedselectreactor?

Is it right for reactor.stop to return before the shutdown is complete?

-- 
Toby Dickenson




More information about the Twisted-Python mailing list