Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 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?


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

print 'Preparing GLib'
print 'Staring Reactor'
from twisted.internet import reactor`

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

share|improve this question
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 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

from twisted.internet import reactor

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 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 Hill 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 is called. – Aaron Hill 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 Hill Jun 27 '12 at 14:59

Your Answer


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.