[Twisted-Python] integrating threadedselectreactor w/ pygtk

David E. Konerding dekonerding at lbl.gov
Tue Nov 22 11:48:01 EST 2005


Hi,

I am attempting to use the threadedselectreactor with pygtk.  I didn't
see a formula for using pygtk
w/ tsr, so this I came up with the code below.  From what I can tell,
pygtk lets you call GUI functions from any thread so long as the
gtk event loop lock is held, and there is no equivalent to "callFromThread".
The alternative seems to be to generate a signal in the CallAfter
callback and emit it, but I can't see why that
would be necessary.  Can anybody comment? 


from twisted.internet import threadedselectreactor
threadedselectreactor.install()
from twisted.internet import reactor

def CallAfter(func):
    gtk.threads_enter()
    func()
    gtk.threads_leave()
  
def foreignEventLoopStop():
    gtk.threads_enter()
    func()
    gtk.threads_leave()

def main():
    gtk.threads_init()
    gtk.gdk.threads_init()
    app = MyApp()
    reactor.interleave(CallAfter)
    reactor.addSystemEventTrigger('after', 'shutdown', foreignEventLoopStop)
    gtk.main()

main()

The problem is that if I send control-C to the console running
the application, I do get the following before my foreignEventLoop Stop

  File
"/home/dek/sw/i386/suse/9.3/pygtk/Python-2.4.2/lib/python2.4/site-packages/twisted/internet/base.py",
line 400, in _continueSystemEvent
    callable(*args, **kw)
  File
"/home/dek/sw/i386/suse/9.3/pygtk/Python-2.4.2/lib/python2.4/site-packages/twisted/internet/threadedselectreactor.py",
line 283, in _mainLoopShutdown
    self.workerThread.join()
  File
"/home/dek/sw/i386/suse/9.3/pygtk/Python-2.4.2/lib/python2.4/threading.py",
line 532, in join
    assert self is not currentThread(), "cannot join current thread"
exceptions.AssertionError: cannot join current thread


Therefore, I tried the following isntead:

def CallAfter(func):
    gobject.idle_add(func)

def foreignEventLoopStop():
    gobject.idle_add(gtk.main_quit)
   
def main():
    gobject.threads_init()
    gtk.threads_init()
    gtk.gdk.threads_init()
    myapp = App
    reactor.interleave(CallAfter)
    reactor.callWhenRunning(createShellServer)
    reactor.addSystemEventTrigger('after', 'shutdown', foreignEventLoopStop)
    gtk.main()
main()

And this seems to work perfectly.




More information about the Twisted-Python mailing list