[Twisted-Python] wxPython locking the event processing?

Philippe Lafoucrière lafou at wanadoo.fr
Sat Jul 5 03:29:06 MDT 2003


On Sat, 2003-07-05 at 10:52, Patrik Blommaskog wrote:
> Hi list,
> 
> Using Twisted/wxPython/Win2k, it seems to me like Twisted's event processing
> gets locked up while I'm moving around in a menu (without selecting
> anyting). For example, if there is a callLater() pending, but the menu bar
> is activated when the time occurs, the call is not made until I leave the
> menu.
> 
> The problem is easily demonstrated with the Twisted wxPython demo found at:
> 
> 	http://twistedmatrix.com/documents/examples/wxdemo.py
> 
> Start the app, then click on the "File" menu. Don't select anything, just
> leave the menu there. Now, the output to stdout is suspended. Release the
> menu and the output resumes.
> 
> Now, I would really like for my model to keep working in the background
> while I'm moving around in the GUI.
> 
> Any suggestions?
> 
> - Patrik

try this

from wxPython.wx import *

#~ if sys.platform == 'win32':
    #~ from twisted.internet import win32eventreactor
    #~ win32eventreactor.install()

from twisted.internet import reactor
from twisted.python import threadable
threadable.init(1)
import threading

def startTwistedAndWx(app):
    """Start twisteds mainloop it its own thread, run the wx mainloop
    in the main thread"""
    global twistedthread
    reactor.startRunning()
    twistedthread = threading.Thread(target=reactor.mainLoop)
    twistedthread.start()
    app.MainLoop()

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



ID_EXIT  = 101

class MyFrame(wxFrame):
    def __init__(self, parent, ID, title):
        wxFrame.__init__(self, parent, ID, title, wxDefaultPosition,
wxSize(300, 200))
        menu = wxMenu()
        menu.Append(ID_EXIT, "E&xit", "Terminate the program")
        menuBar = wxMenuBar()
        menuBar.Append(menu, "&File");
        self.SetMenuBar(menuBar)
        EVT_MENU(self, ID_EXIT,  self.DoExit)
        EVT_CLOSE(self, self.OnCloseWindow)
        self.text = wxTextCtrl(self, -1, "Text\n", style=wxTE_MULTILINE)
        reactor.callLater(0, self.twistedCallsMe, 0)
        
    def DoExit(self, event):
        self.Close(true)

    def OnCloseWindow(self, event):
        stopTwisted()
        self.Destroy()
    
    def twistedCallsMe(self, count):
        print count
        self.text.AppendText('%d\r\n' % count)
        reactor.callLater(0.5, self.twistedCallsMe, count + 1)
        
class MyApp(wxApp):
    def OnInit(self):
        # Do whatever you need to do here
        self.frame = MyFrame(NULL, -1, "Hello, world")
        self.frame.Show(true)
        self.SetTopWindow(self.frame)
        return true

def demo():
    app = MyApp(0)
    startTwistedAndWx(app)

if __name__ == '__main__':
    demo()





More information about the Twisted-Python mailing list