Event-Driven Woes: making wxPython and Twisted work together

D

Daniel Bickett

Hello,

I am writing an application using two event-driven libraries:
wxPython, and twisted. The first problem I encountered in the program
is the confliction between the two all-consuming methods of the two
libraries: app.MainLoop, and reactor.run. Additionally, the fact that
wxPython was to receive requests from the twisted framework as well as
the end user seemed to be simply asking for trouble.

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. I instinctively turned to threading, however
on top of the trouble that this has caused on its own, it has
repeatedly been suggested by the twisted IRC channel not to do this.
After much dwelling on the issue, I have resolved to turn to c.l.py,
to see if anyone had a solution to this problem.

Any help would be very much appreciated,

Daniel Bickett
 
M

matiu

Hi Daniel,

I went down that road for a long time. I made a recipe to help people
out:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/286201

Don't be intimidated by the size, that's a whole demo app complete with
gui. It works quite well. It uses two threads, wx runs in the main
thread and twisted runs in the second thread.

You'll probably just want to glean solutions from it and write your own
framework. It does still have some hicups!

I gave up trying to perfectionalise it and switched to pygtk, glade and
libglade.

pygtk runs fine on both linux and windows, and you get a gui designer.
The only downside is that it has less widgets. It has the advantage of
themes though, this means on windows you can make it look like windows,
linux, mac or whatever. Also your app will load faster than a wx app.
Unfortunately it does take up more disk space though.

Anyway to make wx and twisted work together, put twisted in a secondary
thread. I tried making them co-exist in a single thread as some other
recipes suggest, but basically modal dialogs don't seem to work
properly.

This recipe might be an easier solution for you. Haven't tried it
though:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/203471
God Bless
 
D

David Bolen

Daniel Bickett said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top