[Twisted-Python] wxPython

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?

On Tue, 2005-02-08 at 19:47 +0100, Neal Nelson wrote:
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 ways to use Twisted with wx (e.g. using two threads) but unfortunately I could get neither wxreactor nor wxsupport nor to work reliably across Linux and Win32. So, yes, but you need to do extra work - the python cookbook has a few suggestions.

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
Try the original recipe from the python cookbook. It works for me across platforms. I'm using 80ms for the timer. As Itamar put it, it's not perfectly reliable and usually needs tweaking with the timer. I found values between 80 and 120 ms to be working. Writing a real reactor is at least very complicated if not impossible. Wx uses the internal even loops of the platforms underlying graphical toolkit, so this means the whole thing works differently when using GTK or GTK2 or Windows Controls or Mac... etc. So the reactor code would have to be smart enough to detect the platform and hook into the graphical system's eventloop. Windows uses multiple event loops for modal dialogs, so whenever you open a modal window on windows a secondary eventloop handles that one window and the primary eventloop is stuck until the modal window closes. Same goes for pulldown menues.
Uwe
On Tuesday 08 February 2005 11:34 am, Itamar Shtull-Trauring wrote:
On Tue, 2005-02-08 at 19:47 +0100, Neal Nelson wrote:
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 ways to use Twisted with wx (e.g. using two threads) but unfortunately I could get neither wxreactor nor wxsupport nor to work reliably across Linux and Win32. So, yes, but you need to do extra work
- the python cookbook has a few suggestions.
Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
- -- UC
- -- Open Source Solutions 4U, LLC 2570 Fleetwood Drive Phone: +1 650 872 2425 San Bruno, CA 94066 Cell: +1 650 302 2405 United States Fax: +1 650 872 2417

Neal Nelson nealie@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

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.
In general I think trying to mix two event loops is a bad approach, especially if you have timers, idle events and the like. You always end up having one loop starving the other, unless you do some busy waiting which eats up your CPU.
I'm using another approach which is two have two threads, one for the wx event loop and one for the Twisted event loop. The two threads communicate quite simply using some proxy objects which forward method calls.
I've written a little example of this approach here: http://solipsis.netofpeers.net/wiki/wikka.php?wakka=WxTwistedExample
Regards
Antoine.
participants (5)
-
Antoine Pitrou
-
David Bolen
-
Itamar Shtull-Trauring
-
Neal Nelson
-
Uwe C. Schroeder