Choosing a Reactor and GUI Toolkit Integration

Overview

Twisted provides a variety of implementations of the twisted.internet.reactor. The specialized implementations are suited for different purposes and are designed to integrate better with particular platforms.

The general purpose reactor implementations are:

Platform-specific reactor implementations exist for:

The remaining custom reactor implementations provide support for integrating with the native event loops of various graphical toolkits. This lets your Twisted application use all of the usual Twisted APIs while still being a graphical application.

Twisted currently integrates with the following graphical toolkits:

The support for GTK+, Java, and Qt is straightforward and simple through the use of specialized reactors.

General Purpose Reactors

Select()-based Reactor

The SelectReactor is the default reactor.

from twisted.internet import reactor

The SelectReactor may be explicitly installed by:

from twisted.internet import default
default.install()

Poll()-based Reactor

The PollReactor will work on any platform that provides poll(). With larger numbers of connected sockets, it may provide for better performance.

from twisted.internet import pollreactor
pollreactor.install()

Platform-Specific Reactors

cReactor for Unix

The cReactor is a high-performance C implementation of the Reactor interfaces. It is currently experimental and under active development. Be sure to see the installation notes prior to using the cReactor.

from twisted.internet import cReactor
cReactor.install()

Java

The Java Reactor allows Twisted to run under Jython. It does not currently support AWT or Swing integration.

from twisted.internet import javareactor
javareactor.install()

Win32

The Win32 reactor is not yet complete and has various limitations and issues that need to be addressed. One of the limitations is that it doesn't yet support GUI events, so it is currently not very useful for GUI applications. Contributed improvements in this area are very welcome.

from twisted.internet import win32eventreactor
win32eventreactor.install()

GUI Integration Reactors

GTK+

Twisted integrates with PyGTK. Sample applications using GTK+ and Twisted are available in the Twisted CVS.

from twisted.internet import gtkreactor
gtkreactor.install()

Non-Reactor GUI Integration

Qt

An example Twisted application that uses Qt can be found in doc/examples/qtdemo.py.

from qt import QApplication
app = QApplication([])

from twisted.internet import qtsupport, reactor

# make sure QT quits when twisted is stopped
reactor.addSystemEventTrigger('after', 'shutdown', app.quit )

# install QT support
qtsupport.install(app)

# run reactor
reactor.run()

Tkinter

The support for Tkinter doesn't use a specialized reactor. Instead, there is some specialized support code:

from tkinter import *
from twisted.internet import tksupport

root = Tk()
root.withdraw()

# Install the Reactor support
tksupport.install(root)

# Set up the interface here

# Enter the Tk main loop
mainloop()

An example Twisted application that uses Tk can be found in twisted/words/ui/tkim.py.

WxPython

As with Tkinter, the support for integrating Twisted with a WxPython application uses specialized support code rather than a simple reactor.

from wxPython.wx import *
from twisted.internet import wxsupport, reactor

myWxAppInstance = MyWxApp(0)
wxsupport.install(myWxAppInstance)
reactor.run()

An example Twisted application that uses WxWindows can be found in doc/examples/wxdemo.py.