468,301 Members | 1,462 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,301 developers. It's quick & easy.

Event-Driven Woes: making wxPython and Twisted work together

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
Jul 18 '05 #1
2 2514
Hi Daniel,

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

http://aspn.activestate.com/ASPN/Coo.../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/Coo.../Recipe/203471
God Bless

Jul 18 '05 #2
Daniel Bickett <db******@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/Coo.../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
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Pavils Jurjans | last post: by
8 posts views Thread by Mark | last post: by
3 posts views Thread by Marcia Gulesian | last post: by
7 posts views Thread by Charles Law | last post: by
reply views Thread by Kamilche | last post: by
9 posts views Thread by jeff | last post: by
19 posts views Thread by Daniela Roman | last post: by
4 posts views Thread by tshad | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.