[Twisted-Python] wxPython locking the event processing?

Chris Liechti cliechti at gmx.net
Sat Jul 5 16:20:38 MDT 2003


Patrik Blommaskog wrote:
> I´ve got my app working now with the threading method that Philippe
> suggested. 

it was me that originaly posted that code on this list. and have to say 
that it contains a minor bug. startRunning must be called in the same 
thread as as twisted.mainloop will (otherwise, some nasty things will 
happen, like callFromThread() or stop() not working without an external 
event).
just calling "reactor.run()" in a thread will fail because of the signal 
handlers have to be installed in the main thread, thus these have to be 
disabled.
the stop function can also be improved, so that it does not fail when 
it's called more than once. (wxYield does not like recursive calls to 
itself ;-)

def startTwistedAndWx(app):
     """Start twisteds mainloop it its own thread,
     run the wx mainloop in the main thread"""
     global twistedthread, twistedrunning, twistedapp
     twistedapp = app
     twistedthread = threading.Thread(
         target=reactor.run,
         kwargs={'installSignalHandlers':0}
     )
     twistedthread.start()
     twistedrunning = 1
     app.MainLoop()
     stopTwisted()

def stopTwisted():
     """stop the twisted thread, wx still runs"""
     global twistedthread, twistedrunning
     if twistedrunning:
         twistedrunning = 0
         reactor.stop()
         #wait until reactor has shut down
         while twistedthread.isAlive():
             wxYield()





More information about the Twisted-Python mailing list