[Twisted-Python] Re: wxPython

David Bolen db3l at fitlinxx.com
Tue Feb 8 16:14:31 MST 2005


Neal Nelson <nealie at kobudo.homeunix.net> writes:

> I can't seem to get wxPython to work with Twisted 1.3. I started
> developing a system some time ago but I've only just got back to it
> and the user interface no longer works, which is a bit of a problem.
> 
> Should wx work or is this broken in Twisted?

There are different ways to couple the Twisted and wxPython event
loops, with varying benefits and constraints.  Generally speaking you
need to decide which loop is the master and then handle updating the
other loop from within the main loop.  The wx reactor is an example of
Twisted owning the main loop with periodic updates to the wxPython
message loop.

But at least in my case, the cookbook example of letting wxPython own
the primary loop, and using a periodic timer to crank the Twisted
reactor has worked well.  The biggest negative to this approach (as
has been pointed out in prior threads on the subject) is the lower
granularity, and higher latency, of servicing network operations,
unless you really crank down the timer event and that can drive CPU
up.  But in my case, typically the networking operations are in
support of the GUI rather than the other way around, so our
performance hasn't been affected to any significant degree (and we run
with a 150ms timer).

So, for example, here's a stripped down application class from one of
our applications (using wxPython 2.4 class names):


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)

        # Create appropriate frame
        # ...

        return True
    
    def OnExit(self):
        self.timer.Stop()

        # Stopping the reactor may require multiple stages (separated by
        # a callLater), so the main loop needs to be running when the stop is
        # being executed.
        reactor.callLater(0, reactor.stop)
        reactor.mainLoop()

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


We've used this with Twisted 1.1.0 through 1.3.0, although I haven't
tried the current 2.0 work.


-- David





More information about the Twisted-Python mailing list