choosing-reactor.xhtml   [plain text]


<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Choosing a Reactor and GUI Toolkit Integration</title>
  </head>
  <body>
    <h1>Choosing a Reactor and GUI Toolkit Integration</h1>

    <h2>Overview</h2>

    <p>Twisted provides a variety of implementations of the <code
    class="API">twisted.internet.reactor</code>.  The specialized
    implementations are suited for different purposes and are
    designed to integrate better with particular platforms.</p>

    <p>The general purpose reactor implementations are:</p>

    <ul>
      <li><a href="#select">The select()-based reactor</a></li>
      <li><a href="#poll">The poll()-based reactor</a></li>
    </ul>

    <p>Platform-specific reactor implementations exist for:</p>

    <ul>
      <li><a href="#creactor">cReactor for Unix</a></li>
      <li><a href="#kqueue">KQueue for FreeBSD</a></li>
      <li><a href="#win32">Win32</a></li>
      <li><a href="#cfreactor">Mac OS X</a></li>
    </ul>

    <p>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.</p>

    <p>Twisted currently integrates with the following graphical
    toolkits:</p>

    <ul>
      <li><a href="#gtk">GTK+ 1.2 and 2.0</a></li>
      <li><a href="#qt">Qt</a></li>
      <li><a href="#tkinter">Tkinter</a></li>
      <li><a href="#wxpython">WxPython</a></li>
      <li><a href="#win32">Win32</a></li>
      <li><a href="#cfreactor">Cocoa</a></li>
      <li><a href="#pyui">PyUI</a></li>
    </ul>

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

    <p>In all cases, the event loop is started by calling <code
    class="python">reactor.run()</code>. In all cases, the event loop
    should be stopped with <code
    class="python">reactor.stop()</code>.</p>

    <p><strong>IMPORTANT:</strong> installing a reactor should be the first thing
    done in the app, since any code that does
    <code class="python">from twisted.internet import reactor</code> will automatically
    install the default reactor if the code hasen't already installed one.</p>

    <h2>Reactor Functionality</h2>

    <table cellpadding="7" cellspacing="0" border="1" title="Summary of reactor features">
    <tr><td></td><th>TCP</th><th>SSL</th><th>UDP</th><th>Threading</th><th>Processes</th><th>Scheduling</th><th>Platforms</th></tr>
    <tr><th>select()</th><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y (Unix only)</td><td>Y</td><td>Unix, Win32</td></tr>
    <tr><th>poll()</th><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Unix</td></tr>
    <tr><th>Win32</th><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Win32</td></tr>
    <tr><th>cfreactor</th><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>OS X</td></tr>
    <tr><th>GTK+</th><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y (Unix only)</td><td>Y</td><td>Unix, Win32</td></tr>
    <tr><th>Qt</th><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y (Unix only)</td><td>Y</td><td>Unix, Win32</td></tr>
    <tr><th>kqueue</th><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>FreeBSD</td></tr>
    <tr><th>C</th><td>Y</td><td>N</td><td>N</td><td>Y</td><td>Y</td><td>Y</td><td>Unix</td></tr>
    </table>

    <h2>General Purpose Reactors</h2>

    <h3>Select()-based Reactor</h3><a name="select" />

    <p>The SelectReactor is the default reactor.</p>

<pre class="python">
from twisted.internet import reactor
</pre>

    <p>The SelectReactor may be explicitly installed by:</p>

<pre class="python">
from twisted.internet import default
default.install()
</pre>

    
    <h3>Poll()-based Reactor</h3><a name="poll" />

    <p>The PollReactor will work on any platform that provides
    <code class="python">poll()</code>.  With larger numbers of
    connected sockets, it may provide for better performance.</p>

<pre class="python">
from twisted.internet import pollreactor
pollreactor.install()
</pre>

    <h2>Platform-Specific Reactors</h2>

    <h3>cReactor for Unix</h3><a name="creactor" />

    <p>The cReactor is a high-performance C implementation of the
    Reactor interfaces.  It is currently experimental and under
    active development.</p>

<pre class="python">
from twisted.internet import cReactor
cReactor.install()
</pre>

    <h3>KQueue</h3><a name="kqueue" />

    <p>The KQueue Reactor allows Twisted to use FreeBSD's kqueue mechanism for
       event scheduling. See instructions in the <code class="API"
       >twisted.internet.kqreactor</code>'s
       docstring for installation notes.</p>

<pre class="python">
from twisted.internet import kqreactor
kqreactor.install()
</pre>


   <h3>Win32</h3><a name="win32" />

    <p>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.
    </p>

<pre class="python">
from twisted.internet import win32eventreactor
win32eventreactor.install()
</pre>

    <h2>GUI Integration Reactors</h2>

    <h3>GTK+</h3><a name="gtk" />

    <p>Twisted integrates with <a href="http://www.daa.com.au/~james/pygtk/"
    >PyGTK</a>, versions 1.2 and 2.0.  Sample applications using GTK+ and
    Twisted are available in the Twisted CVS.</p>

<pre class="python">
from twisted.internet import gtkreactor
gtkreactor.install()
</pre>

    <h2>GUI Integration Reactors</h2>

    <h3>Cocoa</h3><a name="cfreactor" />

    <p>Twisted integrates with <a href="http://pyobjc.sf.net/"
    >PyObjC</a>, version 1.0.  Sample applications using Cocoa and Twisted
    are available in the examples directory under <code>Cocoa</code>.</p>

<pre class="python">
from twisted.internet import cfreactor
cfreactor.install()
</pre>

    <h3>Qt</h3><a name="qt" />

    <p>An example Twisted application that uses Qt can be found in
    <code class="py-filename">doc/examples/qtdemo.py</code>.</p>

    <p>When installing the reactor, pass a QApplication instance,
    and if you don't a new one will be created for you.</p>

<pre class="python">
from qt import QApplication
app = QApplication([])

from twisted.internet import qtreactor
qtreactor.install(app)
</pre>

    <h2>Non-Reactor GUI Integration</h2>

    <h3>Tkinter</h3><a name="tkinter" />

    <p>The support for <a href="http://www.python.org/topics/tkinter/"
    >Tkinter</a> doesn't use a specialized reactor.  Instead, there is
    some specialized support code:</p>

<pre class="python">
from Tkinter import *
from twisted.internet import tksupport

root = Tk()
root.withdraw()

# 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()".
</pre>

    <h3>wxPython</h3><a name="wxpython" />

    <p>As with <a href="#tkinter">Tkinter</a>, the support for integrating
    Twisted with a <a href="http://www.wxpython.org">wxPython</a>
    application uses specialized support code rather than a simple reactor.</p>

<pre class="python">
from wxPython.wx import *
from twisted.internet import wxsupport, reactor

myWxAppInstance = wxApp(0)
wxsupport.install(myWxAppInstance)
</pre>

    <p>However, this has issues when runnin 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:</p>

<pre class="python">
from twisted.internet import wxreactor
wxreactor.install()
</pre>

    <p>Later, once a <code class="python">wxApp</code> instance has
    been created, but before <code class="python">reactor.run()</code>
    is called:</p>

<pre class="python">
myWxAppInstance = wxApp(0)
reactor.registerWxApp(myWxAppInstance)
</pre>

    <p>An example Twisted application that uses WxWindows can be found
    in <code class="py-filename">doc/examples/wxdemo.py</code>.</p>

    <h3>PyUI</h3><a name="pyui" />

    <p>As with <a href="#tkinter">Tkinter</a>, the support for integrating
    Twisted with a <a href="http://pyui.sourceforge.net">PyUI</a>
    application uses specialized support code rather than a simple reactor.</p>

<pre class="python">
from twisted.internet import pyuisupport, reactor

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

    <p>An example Twisted application that uses PyUI can bve found in <code
    class="py-filename">doc/examples/pyuidemo.py</code>.</p>

  </body>
</html>