[Twisted-Python] Foreign event loop friendly reactor

Bob Ippolito bob at redivi.com
Sun Apr 17 18:45:47 EDT 2005

On Apr 17, 2005, at 6:21 PM, Shawn Church wrote:

> Bob Ippolito wrote:
>> In response to the "wxpython issues" thread I had suggested that we
>> should have a thread-using reactor that integrates with foreign event
>> loops, so I wrote one and committed it
>> (twisted.internet.threadedselectreactor).  Since this approach
>> requires less maintenance, and whomever maintains the select reactor
>> can trivially maintain this as well, I'm "officially" deprecating
>> cfreactor (although I never really cared much to maintain it anyway).
>> An example of using it to integrate with a foreign event loop is at:
>> doc/core/examples/Cocoa/SimpleWebClient
>> It should be relatively trivial to show how it could be used to
>> integrate with other event loops (wx, Tkinter, pygame, etc.).
>> Effectively, it's compatible with anything that has a thread-safe way
>> to notify the main event loop.
>> In the future, it might make sense to revise the reactor API to
>> support "interleave" directly, and change the existing reactors
>> accordingly so that more than just select can support this behavior.
> Thanks for your work in developing this new reactor.  I just have a
> couple questions:
> Is it (thread) safe to call wx methods from within call/errbacks with
> this reactor?  I ask because I was (based upon the example at
> http://solipsis.netofpeers.net/wiki/wikka.php?wakka=WxTwistedExample)
> translating callbacks into wx.Events executed during the next wx loop
> (the PostEvent function is thread safe).

Forget everything you ever learned about wxPython/Twisted integration.  
It is no longer relevant.  Forget that any of the ugly wxPython+Twisted 
Python Cookbook entries, lame wiki examples, 
twisted.internet.wxreactor, etc. even exist.

> If I am understanding the
> threadedselectreactor.py code correctly it is "waking" the wx loop and
> therefore this should not be needed since only one mainloop will be
> active at a time.

threadedselectreactor lets you imagine that you're in a wonderful 
single threaded world where everything happens in a context where 
calling into Twisted and wxPython are both safe (unless you explicitly 
make life suck by creating more threads).

> Also my understanding is that the reactor.interleave function should be
> called ONCE in order to provide a function to wake the foreign event
> loop.  I am doing this in MyApp.OnInit() and it seems to work fine.  
> The
> example shows this being executed in the frame's __init__ function and 
> I
> do not think this will work for multiple frames (I'm not saying there 
> is
> anything WRONG with the example,  I'm just trying to make sure I
> understand the correct use of the new reactor when dealing with 
> multiple
> wxWindow objects).

Yes it should be called exactly once.  The example does it for the main 
frame.  It is done there because that is the same frame that quits the 
application when it's closed, so it also integrates with reactor.stop.


More information about the Twisted-Python mailing list