Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > media > main-release > by-pkgid > db93d7191b12a3d5ce887da46fc79bf1 > files > 99

python-twisted-core-doc-2.5.0-3mdv2008.1.x86_64.rpm

<?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" lang="en"><head><title>Twisted Documentation: Choosing a Reactor and GUI Toolkit Integration</title><link href="../howto/stylesheet.css" type="text/css" rel="stylesheet" /></head><body bgcolor="white"><h1 class="title">Choosing a Reactor and GUI Toolkit Integration</h1><div class="toc"><ol><li><a href="#auto0">Overview</a></li><li><a href="#auto1">Reactor Functionality</a></li><li><a href="#auto2">General Purpose Reactors</a></li><ul><li><a href="#auto3">Select()-based Reactor</a></li><li><a href="#auto4">Poll()-based Reactor</a></li></ul><li><a href="#auto5">Platform-Specific Reactors</a></li><ul><li><a href="#auto6">KQueue</a></li><li><a href="#auto7">Win32 (WFMO)</a></li><li><a href="#auto8">Win32 (IOCP)</a></li></ul><li><a href="#auto9">GUI Integration Reactors</a></li><ul><li><a href="#auto10">GTK+</a></li><li><a href="#auto11">CoreFoundation</a></li></ul><li><a href="#auto12">Non-Reactor GUI Integration</a></li><ul><li><a href="#auto13">Tkinter</a></li><li><a href="#auto14">wxPython</a></li><li><a href="#auto15">PyUI</a></li></ul></ol></div><div class="content"><span></span><h2>Overview<a name="auto0"></a></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="#kqueue">KQueue for FreeBSD and OS X</a></li><li><a href="#win32_wfmo">Win32 (WFMO)</a></li><li><a href="#win32_iocp">Win32 (IOCP)</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="#tkinter">Tkinter</a></li><li><a href="#wxpython">WxPython</a></li><li><a href="#win32_wfmo">Win32</a></li><li><a href="#cfreactor">CoreFoundation</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<a name="auto1"></a></h2><table cellpadding="7" border="1" cellspacing="0" title="Summary of reactor features"><tr><td></td><th>Status</th><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>Stable</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Unix, Win32</td></tr><tr><th>poll()</th><td>Stable</td><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 (WFMO)</th><td>Experimental</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Win32</td></tr><tr><th>Win32 (IOCP)</th><td>Experimental</td><td>Y</td><td>N</td><td>N</td><td>N</td><td>N</td><td>Y</td><td>Win32</td></tr><tr><th>CoreFoundation</th><td>Unmaintained</td><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>Stable</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Unix, Win32</td></tr><tr><th>wx</th><td>Experimental</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Unix, Win32</td></tr><tr><th>kqueue</th><td>Experimental</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>Y</td><td>FreeBSD</td></tr></table><h2>General Purpose Reactors<a name="auto2"></a></h2><h3>Select()-based Reactor<a name="auto3"></a></h3><a name="select"></a><p>
    The select reactor is currently the default reactor on all platforms. 
    The following code will install it, if no other reactor has been
    installed:
    </p><pre class="python">
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">reactor</span>
</pre><p>
    In the future, if another reactor becomes the default, but the select
    reactor is desired, it may be installed via:
    </p><pre class="python">
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">selectreactor</span>
<span class="py-src-variable">selectreactor</span>.<span class="py-src-variable">install</span>()
</pre><h3>Poll()-based Reactor<a name="auto4"></a></h3><a name="poll"></a><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">
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">pollreactor</span>
<span class="py-src-variable">pollreactor</span>.<span class="py-src-variable">install</span>()
</pre><h2>Platform-Specific Reactors<a name="auto5"></a></h2><h3>KQueue<a name="auto6"></a></h3><a name="kqueue"></a><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">
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">kqreactor</span>
<span class="py-src-variable">kqreactor</span>.<span class="py-src-variable">install</span>()
</pre><h3>Win32 (WFMO)<a name="auto7"></a></h3><a name="win32_wfmo"></a><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">
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">win32eventreactor</span>
<span class="py-src-variable">win32eventreactor</span>.<span class="py-src-variable">install</span>()
</pre><h3>Win32 (IOCP)<a name="auto8"></a></h3><a name="win32_iocp"></a><p>
    Windows provides a fast, scalable event notification system known as IO
    Completion Ports, or IOCP for short.  An extremely experimental reactor
    based on IOCP is provided with Twisted.
    </p><pre class="python">
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">iocpreactor</span>
<span class="py-src-variable">iocpreactor</span>.<span class="py-src-variable">install</span>()
</pre><h2>GUI Integration Reactors<a name="auto9"></a></h2><h3>GTK+<a name="auto10"></a></h3><a name="gtk"></a><p>Twisted integrates with <a href="http://www.daa.com.au/~james/pygtk/">PyGTK</a>, versions 1.2 (<code>gtkreactor</code>) and 2.0
    (<code>gtk2reactor</code>). Sample applications using GTK+ and
    Twisted are available in the Twisted SVN.</p><p>GTK-2.0 split the event loop out of the GUI toolkit, into a separate
    module called <q>glib</q>. To run an application using the glib event
    loop, use the <code>glib2reactor</code>. This will be slightly faster
    than <code>gtk2reactor</code> (and does not require a working X display),
    but cannot be used to run GUI applications.</p><pre class="python">
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">gtkreactor</span> <span class="py-src-comment"># for gtk-1.2</span>
<span class="py-src-variable">gtkreactor</span>.<span class="py-src-variable">install</span>()
</pre><pre class="python">
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">gtk2reactor</span> <span class="py-src-comment"># for gtk-2.0</span>
<span class="py-src-variable">gtk2reactor</span>.<span class="py-src-variable">install</span>()
</pre><pre class="python">
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">glib2reactor</span> <span class="py-src-comment"># for non-GUI apps</span>
<span class="py-src-variable">glib2reactor</span>.<span class="py-src-variable">install</span>()
</pre><h3>CoreFoundation<a name="auto11"></a></h3><a name="cfreactor"></a><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">
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">cfreactor</span>
<span class="py-src-variable">cfreactor</span>.<span class="py-src-variable">install</span>()
</pre><h2>Non-Reactor GUI Integration<a name="auto12"></a></h2><h3>Tkinter<a name="auto13"></a></h3><a name="tkinter"></a><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">
<span class="py-src-keyword">from</span> <span class="py-src-variable">Tkinter</span> <span class="py-src-keyword">import</span> *
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">tksupport</span>

<span class="py-src-variable">root</span> = <span class="py-src-variable">Tk</span>()

<span class="py-src-comment"># Install the Reactor support
</span><span class="py-src-variable">tksupport</span>.<span class="py-src-variable">install</span>(<span class="py-src-variable">root</span>)

<span class="py-src-comment"># at this point build Tk app as usual using the root object,
</span><span class="py-src-comment"># and start the program with &quot;reactor.run()&quot;, and stop it
</span><span class="py-src-comment"># with &quot;reactor.stop()&quot;.
</span></pre><h3>wxPython<a name="auto14"></a></h3><a name="wxpython"></a><p>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.</p><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">
<span class="py-src-keyword">from</span> <span class="py-src-variable">wxPython</span>.<span class="py-src-variable">wx</span> <span class="py-src-keyword">import</span> *
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">wxsupport</span>, <span class="py-src-variable">reactor</span>

<span class="py-src-variable">myWxAppInstance</span> = <span class="py-src-variable">wxApp</span>(<span class="py-src-number">0</span>)
<span class="py-src-variable">wxsupport</span>.<span class="py-src-variable">install</span>(<span class="py-src-variable">myWxAppInstance</span>)
</pre><p>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:</p><pre class="python">
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">wxreactor</span>
<span class="py-src-variable">wxreactor</span>.<span class="py-src-variable">install</span>()
</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">
<span class="py-src-variable">myWxAppInstance</span> = <span class="py-src-variable">wxApp</span>(<span class="py-src-number">0</span>)
<span class="py-src-variable">reactor</span>.<span class="py-src-variable">registerWxApp</span>(<span class="py-src-variable">myWxAppInstance</span>)
</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<a name="auto15"></a></h3><a name="pyui"></a><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">
<span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span>.<span class="py-src-variable">internet</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">pyuisupport</span>, <span class="py-src-variable">reactor</span>

<span class="py-src-variable">pyuisupport</span>.<span class="py-src-variable">install</span>(<span class="py-src-variable">args</span>=(<span class="py-src-number">640</span>, <span class="py-src-number">480</span>), <span class="py-src-variable">kw</span>={<span class="py-src-string">'renderer'</span>: <span class="py-src-string">'gl'</span>})
</pre><p>An example Twisted application that uses PyUI can bve found in <code class="py-filename">doc/examples/pyuidemo.py</code>.</p></div><p><a href="../howto/index.html">Index</a></p><span class="version">Version: 2.5.0</span></body></html>