[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 
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 

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

Is there a better approach?

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

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

Toby Dickenson

More information about the Twisted-Python mailing list