Twisted Documentation: Choosing a Reactor and GUI Toolkit Integration

# 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:

- The select()-based reactor
- The poll()-based reactor

Platform-specific reactor implementations exist for:

- KQueue for FreeBSD and OS X
- Win32 (WFMO)
- Win32 (IOCP)
- Mac OS X
- Epoll for Linux 2.6

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:

- GTK+ 1.2 and 2.0
- Tkinter
- WxPython
- Win32
- CoreFoundation
- PyUI

When using applications that runnable using `twistd`, e.g. TAPs or plugins, there is no need to choose a reactor explicitly, since this can be chosen using `twistd`'s -r option.

In all cases, the event loop is started by calling `reactor.run()`. In all cases, the event loop should be stopped with `reactor.stop()`.

IMPORTANT: installing a reactor should be the first thing done in the app, since any code that does `from twisted.internet import reactor` will automatically install the default reactor if the code hasn't already installed one.

## Reactor Functionality

| Status | TCP | SSL | UDP | Threading | Processes | Scheduling | Platforms
select() | Stable | Y | Y | Y | Y | Y | Y | Unix, Win32
poll() | Stable | Y | Y | Y | Y | Y | Y | Unix
Win32 (WFMO) | Experimental | Y | Y | Y | Y | Y | Y | Win32
Win32 (IOCP) | Experimental | Y | Y | N | N | N | Y | Win32
CoreFoundation | Unmaintained | Y | Y | Y | Y | Y | Y | OS X
epoll | Stable | Y | Y | Y | Y | Y | Y | Linux 2.6
Gtk | Stable | Y | Y | Y | Y | Y | Y | Unix, Win32
wx | Experimental | Y | Y | Y | Y | Y | Y | Unix, Win32
kqueue | Experimental | Y | Y | Y | Y | Y | Y | FreeBSD

## General Purpose Reactors

### Select()-based Reactor

The `select` reactor is currently the default reactor on all platforms. The following code will install it, if no other reactor has been installed:

    1
    from twisted.internet import reactor

In the future, if another reactor becomes the default, but the `select` reactor is desired, it may be installed via:

    1
    2
    3
    4
    from twisted.internet import selectreactor
    selectreactor.install()

    from twisted.internet import reactor

### Poll()-based Reactor

The PollReactor will work on any platform that provides `poll()` (while OS X provides `poll()`, it is not recommended to use the PollReactor on OS X due to bugs in its implementation of the call). With larger numbers of connected sockets, it may provide for better performance than the SelectReactor.

    1
    2
    3
    4
    from twisted.internet import pollreactor
    pollreactor.install()

    from twisted.internet import reactor

## Platform-Specific Reactors

### KQueue

The KQueue Reactor allows Twisted to use FreeBSD's kqueue mechanism for event scheduling. See instructions in the `twisted.internet.kqreactor`'s docstring for installation notes.

    1
    2
    3
    4
    from twisted.internet import kqreactor
    kqreactor.install()

    from twisted.internet import reactor

### Win32 (WFMO)

The Win32 reactor is not yet complete and has various limitations and issues that need to be addressed. The reactor supports GUI integration with the win32gui module, so it can be used for native Win32 GUI applications.

    1
    2
    3
    4
    from twisted.internet import win32eventreactor
    win32eventreactor.install()

    from twisted.internet import reactor

### Win32 (IOCP)

Windows provides a fast, scalable event notification system known as IO Completion Ports, or IOCP for short. Twisted includes a reactor based on IOCP which is nearly complete. The reactor has a handful of known bugs and lacks SSL support.

    1
    2
    3
    4
    from twisted.internet import iocpreactor
    iocpreactor.install()

    from twisted.internet import reactor

### Epoll-based Reactor

The EPollReactor will work on any platform that provides `epoll`, today only Linux 2.6 and over. The implementation of the epoll reactor currently uses the Level Triggered interface, which is basically like poll() but scales much better.

    1
    2
    3
    4
    from twisted.internet import epollreactor
    epollreactor.install()

    from twisted.internet import reactor

## GUI Integration Reactors

### GTK+

Twisted integrates with PyGTK, versions 1.2 (`gtkreactor`) and 2.0 (`gtk2reactor`). Sample applications using GTK+ and Twisted are available in the Twisted SVN.

GTK-2.0 split the event loop out of the GUI toolkit, into a separate module called glib. To run an application using the glib event loop, use the `glib2reactor`. This will be slightly faster than `gtk2reactor` (and does not require a working X display), but cannot be used to run GUI applications.

    1
    2
    3
    4
    from twisted.internet import gtkreactor # for gtk-1.2
    gtkreactor.install()

    from twisted.internet import reactor

    1
    2
    3
    4
    from twisted.internet import gtk2reactor # for gtk-2.0
    gtk2reactor.install()

    from twisted.internet import reactor

    1
    2
    3
    4
    from twisted.internet import glib2reactor # for non-GUI apps
    glib2reactor.install()

    from twisted.internet import reactor

### CoreFoundation

Twisted integrates with PyObjC, version 1.0. Sample applications using Cocoa and Twisted are available in the examples directory under `threadedselect/Cocoa`.

    1
    2
    3
    4
    from twisted.internet import cfreactor
    cfreactor.install()

    from twisted.internet import reactor

## Non-Reactor GUI Integration

### Tkinter

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

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    from Tkinter import *
    from twisted.internet import tksupport

    root = Tk()

    # Install the Reactor support
    tksupport.install(root)

    # at this point build Tk app as usual using the root object,
    # and start the program with "reactor.run()", and stop it
    # with "reactor.stop()".

### wxPython

Twisted currently supports two methods of integrating wxPython. Unfortunately, neither method will work on all wxPython platforms (such as GTK2 or Windows). It seems that the only portable way to integrate with wxPython is to run it in a separate thread. One of these methods may be sufficient if your wx app is limited to a single platform.

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

    1
    2
    3
    4
    5
    from wxPython.wx import *
    from twisted.internet import wxsupport, reactor

    myWxAppInstance = wxApp(0)
    wxsupport.install(myWxAppInstance)

However, this has issues when running on Windows, so Twisted now comes with alternative wxPython support using a reactor. Using this method is probably better. Initialization is done in two stages. In the first, the reactor is installed:

    1
    2
    from twisted.internet import wxreactor
    wxreactor.install()

Later, once a `wxApp` instance has been created, but before `reactor.run()` is called:

    1
    2
    myWxAppInstance = wxApp(0)
    reactor.registerWxApp(myWxAppInstance)

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

### PyUI

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

    1
    2
    3
    from twisted.internet import pyuisupport, reactor

    pyuisupport.install(args=(640, 480), kw={'renderer': 'gl'})

An example Twisted application that uses PyUI can be found in `doc/examples/pyuidemo.py`.

Index

Version: 10.0.0