Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I am creating a Quickly application that makes use of Python Twisted. When the user launches a window, I want to start the Twisted server I wrote. However, when I do this, it freezes the GUI, because the reactor.run method is constantly running after it is started. I need a way to start the Twisted reactor in its own thread, so that the GUI will continue to operate while the reactor is running. I have read about using Twisted with pyGTK in the Twisted documentation, but I am not sure how I would accomplish this with a Quickly application. What is the best way to start my Twisted server when a window opens without freezing the GUI?

Update:

My if __name__ == '__main__' statement at the end of my dialog file is:

print 'Preparing GLib'
GLib.idle_add(before_running)
print 'Staring Reactor'
from twisted.internet import reactor
reactor.run()`

I have imported and installed twisted.gtk3reactor earlier on in the file. The before_running method contains dialog = ClientDialog() and dialog.show, as well as the rest of the code I want to run when the dialog opens.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

If you are using the python-gi bindings on 12.04 or newer, you need to import and install the twisted.internet.gireactor or twisted.internet.gtk3reactor, before doing the reactor.run(). You also need to use this as your main loop, rather than running Gtk.main() or a GLib.MainLoop().run() yourself. If you are on an older version and using the old PyGTK+ bindings, then you need to use either glib2reactor or gtk2reactor.

Something like this:

from twisted.internet import gtk3reactor
from gi.repository import GLib
gtk3reactor.install()

GLib.idle_add(create_windows_and_do_stuff_method)
from twisted.internet import reactor

reactor.run()

The sequence is important, as importing the default reactor will result in it being installed, rather than the appropriate reactor, if a reactor is not already installed. Thus you need to install the proper one first, then do the reactor.run(). And instead of Gtk.main_quit(), you should from twisted.internet import reactor and use reactor.stop().

share|improve this answer
    
I followed your instructions, but they did not work. I created an instance of twisted.internet.gireactor.GIReactor class, and ran its run method instead of running Gtk.main() in the if _name_ == '_main_': statement at the bottom of the code for my dialog, but it did not work. I put a print statement right before the listenTCP and run method calls, but the print statement never ran. What did I do wrong? –  Aaron Jun 26 '12 at 23:55
    
You shouldn't create your own instance of the class in that way. I've updated the answer with a code example, to help you understand better. –  dobey Jun 27 '12 at 13:15
    
Where I should I the reactor.listenTCP function call? In your example, only reactor.run() is called. –  Aaron Jun 27 '12 at 14:05
    
In the "create_windows_and_do_stuff_method" or somewhere else that is called from in there. –  dobey Jun 27 '12 at 14:23
    
I updated my question to show the changes I have made. The print 'Preparing GLib' statement is never run. –  Aaron Jun 27 '12 at 14:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.