Event-Driven Woes: making wxPython and Twisted work together

David Bolen db3l at fitlinxx.com
Thu Dec 30 23:05:21 CET 2004


Daniel Bickett <dbickett at gmail.com> writes:

> My initial solution was, naturally, the wxPython support inside of the
> twisted framework. However, it has been documented by the author that
> the support is unstable at this time, and should not be used in
> full-scale applications.

Rather than the wx reactor, there's an alternate recipe that just
cranks the twisted event loop from within a timer at the wx level that
we've used very successfully.  It does have some caveats (such as a
potentially higher latency in servicing the network based on your
timer interval), but so far for our applications it hasn't been an
issue at all, so it might be something you might try.  The code was
based on http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/181780

The advantage of this approach is that no threads are necessary, and
thus there's no problem issuing wxPython calls from within twisted
callbacks or twisted calls from within wxPython event handlers.

Just include the following bits into your application object (note that
the use of "installSignalHandlers" might not be needed on all systems):

class MyApp(wx.wxApp):

    (...)

    def OnInit(self):

        # Twisted Reactor code
        reactor.startRunning(installSignalHandlers=0)
        wx.EVT_TIMER(self, 999999, self.OnTimer)
        self.timer = wx.wxTimer(self, 999999)
        self.timer.Start(150, False)

        (...)

    def OnTimer(self, event):
        reactor.runUntilCurrent()
        reactor.doIteration(0)

    def __del__(self):
        self.timer.Stop()
        reactor.stop()
        wx.wxApp.__del__(self)


and you can try adjusting the timer interval for the best mix of CPU
load versus latency.

-- David



More information about the Python-list mailing list