Initial import to Tizen
[profile/ivi/python-twisted.git] / doc / core / howto / choosing-reactor.html
1 <?xml version="1.0" encoding="utf-8"?><!DOCTYPE html  PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN'  'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html lang="en" xmlns="http://www.w3.org/1999/xhtml">
2   <head>
3 <title>Twisted Documentation: Choosing a Reactor and GUI Toolkit Integration</title>
4 <link href="stylesheet.css" rel="stylesheet" type="text/css"/>
5   </head>
6
7   <body bgcolor="white">
8     <h1 class="title">Choosing a Reactor and GUI Toolkit Integration</h1>
9     <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></ul><li><a href="#auto4">Platform-Specific Reactors</a></li><ul><li><a href="#auto5">Poll-based Reactor</a></li><li><a href="#auto6">KQueue</a></li><li><a href="#auto7">WaitForMultipleObjects (WFMO) for Win32</a></li><li><a href="#auto8">Input/Output Completion Port (IOCP) for Win32</a></li><li><a href="#auto9">Epoll-based Reactor</a></li></ul><li><a href="#auto10">GUI Integration Reactors</a></li><ul><li><a href="#auto11">GTK+</a></li><li><a href="#auto12">GTK+ 3.0 and GObject Introspection</a></li><li><a href="#auto13">wxPython</a></li><li><a href="#auto14">CoreFoundation</a></li></ul><li><a href="#auto15">Non-Reactor GUI Integration</a></li><ul><li><a href="#auto16">Tkinter</a></li><li><a href="#auto17">PyUI</a></li></ul></ol></div>
10     <div class="content">
11     <span/>
12
13     <h2>Overview<a name="auto0"/></h2>
14
15     <p>Twisted provides a variety of implementations of the <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.internet.reactor.html" title="twisted.internet.reactor">twisted.internet.reactor</a></code>.  The specialized
16     implementations are suited for different purposes and are
17     designed to integrate better with particular platforms.</p>
18
19     <p>The <a href="#epoll" shape="rect">epoll()-based reactor</a> is Twisted's default on
20     Linux. Other platforms use <a href="#poll" shape="rect">poll()</a>, or the most
21     cross-platform reactor, <a href="#select" shape="rect">select()</a>.</p>
22
23     <p>Platform-specific reactor implementations exist for:</p>
24
25     <ul>
26       <li><a href="#poll" shape="rect">Poll for Linux</a></li>
27       <li><a href="#epoll" shape="rect">Epoll for Linux 2.6</a></li>
28       <li><a href="#win32_wfmo" shape="rect">WaitForMultipleObjects (WFMO) for Win32</a></li>
29       <li><a href="#win32_iocp" shape="rect">Input/Output Completion Port (IOCP) for Win32</a></li>
30       <li><a href="#kqueue" shape="rect">KQueue for FreeBSD and Mac OS X</a></li>
31       <li><a href="#cfreactor" shape="rect">CoreFoundation for Mac OS X</a></li>
32     </ul>
33
34     <p>The remaining custom reactor implementations provide support
35     for integrating with the native event loops of various graphical
36     toolkits.  This lets your Twisted application use all of the
37     usual Twisted APIs while still being a graphical application.</p>
38
39     <p>Twisted currently integrates with the following graphical
40     toolkits:</p>
41
42     <ul>
43       <li><a href="#gtk" shape="rect">GTK+ 2.0</a></li>
44       <li><a href="#gtk3" shape="rect">GTK+ 3.0 and GObject Introspection</a></li>
45       <li><a href="#tkinter" shape="rect">Tkinter</a></li>
46       <li><a href="#wxpython" shape="rect">wxPython</a></li>
47       <li><a href="#win32_wfmo" shape="rect">Win32</a></li>
48       <li><a href="#cfreactor" shape="rect">CoreFoundation</a></li>
49       <li><a href="#pyui" shape="rect">PyUI</a></li>
50     </ul>
51
52     <p>When using applications that are runnable using <code>twistd</code>, e.g.
53        TACs or plugins, there is no need to choose a reactor explicitly, since
54        this can be chosen using <code>twistd</code>'s -r option.</p>
55
56     <p>In all cases, the event loop is started by calling <code class="python">reactor.run()</code>. In all cases, the event loop
57     should be stopped with <code class="python">reactor.stop()</code>.</p>
58
59     <p><strong>IMPORTANT:</strong> installing a reactor should be the first thing
60     done in the app, since any code that does
61     <code class="python">from twisted.internet import reactor</code> will automatically
62     install the default reactor if the code hasn't already installed one.</p>
63
64     <h2>Reactor Functionality<a name="auto1"/></h2>
65
66     <table border="1" cellpadding="7" cellspacing="0" title="Summary of reactor features">
67     <tr><td colspan="1" rowspan="1"/><th colspan="1" rowspan="1">Status</th><th colspan="1" rowspan="1">TCP</th><th colspan="1" rowspan="1">SSL</th><th colspan="1" rowspan="1">UDP</th><th colspan="1" rowspan="1">Threading</th><th colspan="1" rowspan="1">Processes</th><th colspan="1" rowspan="1">Scheduling</th><th colspan="1" rowspan="1">Platforms</th></tr>
68     <tr><th colspan="1" rowspan="1">select()</th><td colspan="1" rowspan="1">Stable</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Unix, Win32</td></tr>
69     <tr><th colspan="1" rowspan="1">poll</th><td colspan="1" rowspan="1">Stable</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Unix</td></tr>
70     <tr><th colspan="1" rowspan="1">WaitForMultipleObjects (WFMO) for Win32</th><td colspan="1" rowspan="1">Experimental</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Win32</td></tr>
71     <tr><th colspan="1" rowspan="1">Input/Output Completion Port (IOCP) for Win32</th><td colspan="1" rowspan="1">Experimental</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Win32</td></tr>
72     <tr><th colspan="1" rowspan="1">CoreFoundation</th><td colspan="1" rowspan="1">Unmaintained</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Mac OS X</td></tr>
73     <tr><th colspan="1" rowspan="1">epoll</th><td colspan="1" rowspan="1">Stable</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Linux 2.6</td></tr>
74     <tr><th colspan="1" rowspan="1">GTK+</th><td colspan="1" rowspan="1">Stable</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Unix, Win32</td></tr>
75     <tr><th colspan="1" rowspan="1">wx</th><td colspan="1" rowspan="1">Experimental</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Unix, Win32</td></tr>
76     <tr><th colspan="1" rowspan="1">kqueue</th><td colspan="1" rowspan="1">Experimental</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">Y</td><td colspan="1" rowspan="1">FreeBSD</td></tr>
77     </table>
78
79     <h2>General Purpose Reactors<a name="auto2"/></h2>
80
81     <h3>Select()-based Reactor<a name="auto3"/></h3><a name="select" shape="rect"/>
82
83     <p>The <code>select</code> reactor is the default on platforms that don't
84     provide a better alternative that covers all use cases. If
85     the <code>select</code> reactor is desired, it may be installed via:</p>
86
87 <pre class="python"><p class="py-linenumber">1
88 2
89 3
90 4
91 </p><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>
92 <span class="py-src-variable">selectreactor</span>.<span class="py-src-variable">install</span>()
93
94 <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>
95 </pre>
96
97     <h2>Platform-Specific Reactors<a name="auto4"/></h2>
98
99     <h3>Poll-based Reactor<a name="auto5"/></h3><a name="poll" shape="rect"/>
100
101     <p>The PollReactor will work on any platform that provides <code class="python">select.poll</code>.  With larger numbers of connected
102     sockets, it may provide for better performance than the SelectReactor.</p>
103
104 <pre class="python"><p class="py-linenumber">1
105 2
106 3
107 4
108 </p><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>
109 <span class="py-src-variable">pollreactor</span>.<span class="py-src-variable">install</span>()
110
111 <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>
112 </pre>
113
114     <h3>KQueue<a name="auto6"/></h3><a name="kqueue" shape="rect"/>
115
116     <p>The KQueue Reactor allows Twisted to use FreeBSD's kqueue mechanism for
117        event scheduling. See instructions in the <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.internet.kqreactor.html" title="twisted.internet.kqreactor">twisted.internet.kqreactor</a></code>'s
118        docstring for installation notes.</p>
119
120 <pre class="python"><p class="py-linenumber">1
121 2
122 3
123 4
124 </p><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>
125 <span class="py-src-variable">kqreactor</span>.<span class="py-src-variable">install</span>()
126
127 <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>
128 </pre>
129
130
131    <h3>WaitForMultipleObjects (WFMO) for Win32<a name="auto7"/></h3><a name="win32_wfmo" shape="rect"/>
132
133     <p>The Win32 reactor is not yet complete and has various limitations
134     and issues that need to be addressed.  The reactor supports GUI integration
135     with the win32gui module, so it can be used for native Win32 GUI applications.
136     </p>
137
138 <pre class="python"><p class="py-linenumber">1
139 2
140 3
141 4
142 </p><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>
143 <span class="py-src-variable">win32eventreactor</span>.<span class="py-src-variable">install</span>()
144
145 <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>
146 </pre>
147
148    <h3>Input/Output Completion Port (IOCP) for Win32<a name="auto8"/></h3><a name="win32_iocp" shape="rect"/>
149
150     <p>
151     Windows provides a fast, scalable event notification system known as IO
152     Completion Ports, or IOCP for short.  Twisted includes a reactor based
153     on IOCP which is nearly complete.
154     </p>
155
156 <pre class="python"><p class="py-linenumber">1
157 2
158 3
159 4
160 </p><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>
161 <span class="py-src-variable">iocpreactor</span>.<span class="py-src-variable">install</span>()
162
163 <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>
164 </pre>
165
166     <h3>Epoll-based Reactor<a name="auto9"/></h3><a name="epoll" shape="rect"/>
167
168     <p>The EPollReactor will work on any platform that provides
169     <code class="python">epoll</code>, today only Linux 2.6 and over. The
170     implementation of the epoll reactor currently uses the Level Triggered
171     interface, which is basically like poll() but scales much better.</p>
172
173 <pre class="python"><p class="py-linenumber">1
174 2
175 3
176 4
177 </p><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">epollreactor</span>
178 <span class="py-src-variable">epollreactor</span>.<span class="py-src-variable">install</span>()
179
180 <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>
181 </pre>
182
183     <h2>GUI Integration Reactors<a name="auto10"/></h2>
184
185     <h3>GTK+<a name="auto11"/></h3><a name="gtk" shape="rect"/>
186
187     <p>Twisted integrates with <a href="http://www.pygtk.org/" shape="rect">PyGTK</a> version
188     2.0 using the <code>gtk2reactor</code>. An example Twisted application that
189     uses GTK+ can be found
190     in <code class="py-filename">doc/core/examples/pbgtk2.py</code>.</p>
191
192     <p>GTK-2.0 split the event loop out of the GUI toolkit and into a separate
193     module called <q>glib</q>. To run an application using the glib event loop,
194     use the <code>glib2reactor</code>. This will be slightly faster
195     than <code>gtk2reactor</code> (and does not require a working X display),
196     but cannot be used to run GUI applications.</p>
197
198 <pre class="python"><p class="py-linenumber">1
199 2
200 3
201 4
202 </p><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>
203 <span class="py-src-variable">gtk2reactor</span>.<span class="py-src-variable">install</span>()
204
205 <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>
206 </pre>
207
208 <pre class="python"><p class="py-linenumber">1
209 2
210 3
211 4
212 </p><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>
213 <span class="py-src-variable">glib2reactor</span>.<span class="py-src-variable">install</span>()
214
215 <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>
216 </pre>
217
218     <h3>GTK+ 3.0 and GObject Introspection<a name="auto12"/></h3><a name="gtk3" shape="rect"/>
219
220     <p>Twisted integrates with <a href="http://gtk.org" shape="rect">GTK+ 3</a> and GObject
221     through <a href="http://live.gnome.org/PyGObject" shape="rect">PyGObject's</a>
222     introspection using the <code>gtk3reactor</code>
223     and <code>gireactor</code> reactors.</p>
224
225 <pre class="python"><p class="py-linenumber">1
226 2
227 3
228 4
229 </p><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">gtk3reactor</span>
230 <span class="py-src-variable">gtk3reactor</span>.<span class="py-src-variable">install</span>()
231
232 <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>
233 </pre>
234
235 <pre class="python"><p class="py-linenumber">1
236 2
237 3
238 4
239 </p><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">gireactor</span> <span class="py-src-comment"># for non-GUI apps</span>
240 <span class="py-src-variable">gireactor</span>.<span class="py-src-variable">install</span>()
241
242 <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>
243 </pre>
244
245     <p>GLib 3.0 introduces the concept of <code>GApplication</code>, a class
246     that handles application uniqueness in a cross-platform way and provides
247     its own main loop. Its counterpart <code>GtkApplication</code> also
248     handles application lifetime with respect to open windows. Twisted
249     supports registering these objects with the event loop, which should be
250     done before running the reactor:</p>
251
252 <pre class="python"><p class="py-linenumber">1
253 2
254 3
255 4
256 5
257 6
258 7
259 8
260 9
261 </p><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">gtk3reactor</span>
262 <span class="py-src-variable">gtk3reactor</span>.<span class="py-src-variable">install</span>()
263
264 <span class="py-src-keyword">from</span> <span class="py-src-variable">gi</span>.<span class="py-src-variable">repository</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">Gtk</span>
265 <span class="py-src-variable">app</span> = <span class="py-src-variable">Gtk</span>.<span class="py-src-variable">Application</span>(...)
266
267 <span class="py-src-keyword">from</span> <span class="py-src-variable">twisted</span> <span class="py-src-keyword">import</span> <span class="py-src-variable">reactor</span>
268 <span class="py-src-variable">reactor</span>.<span class="py-src-variable">registerGApplication</span>(<span class="py-src-variable">app</span>)
269 <span class="py-src-variable">reactor</span>.<span class="py-src-variable">run</span>()
270 </pre>
271
272     <h3>wxPython<a name="auto13"/></h3><a name="wxpython" shape="rect"/>
273
274     <p>Twisted currently supports two methods of integrating
275     wxPython. Unfortunately, neither method will work on all wxPython
276     platforms (such as GTK2 or Windows). It seems that the only
277     portable way to integrate with wxPython is to run it in a separate
278     thread. One of these methods may be sufficient if your wx app is
279     limited to a single platform.</p>
280
281     <p>As with <a href="#tkinter" shape="rect">Tkinter</a>, the support for integrating
282     Twisted with a <a href="http://www.wxpython.org" shape="rect">wxPython</a>
283     application uses specialized support code rather than a simple reactor.</p>
284
285 <pre class="python"><p class="py-linenumber">1
286 2
287 3
288 4
289 5
290 </p><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> *
291 <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>
292
293 <span class="py-src-variable">myWxAppInstance</span> = <span class="py-src-variable">wxApp</span>(<span class="py-src-number">0</span>)
294 <span class="py-src-variable">wxsupport</span>.<span class="py-src-variable">install</span>(<span class="py-src-variable">myWxAppInstance</span>)
295 </pre>
296
297     <p>However, this has issues when running on Windows, so Twisted now
298     comes with alternative wxPython support using a reactor. Using
299     this method is probably better. Initialization is done in two
300     stages. In the first, the reactor is installed:</p>
301
302 <pre class="python"><p class="py-linenumber">1
303 2
304 3
305 4
306 </p><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>
307 <span class="py-src-variable">wxreactor</span>.<span class="py-src-variable">install</span>()
308
309 <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>
310 </pre>
311
312     <p>Later, once a <code class="python">wxApp</code> instance has
313     been created, but before <code class="python">reactor.run()</code>
314     is called:</p>
315
316 <pre class="python"><p class="py-linenumber">1
317 2
318 3
319 </p><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>
320 <span class="py-src-variable">myWxAppInstance</span> = <span class="py-src-variable">wxApp</span>(<span class="py-src-number">0</span>)
321 <span class="py-src-variable">reactor</span>.<span class="py-src-variable">registerWxApp</span>(<span class="py-src-variable">myWxAppInstance</span>)
322 </pre>
323
324     <p>An example Twisted application that uses wxPython can be found
325     in <code class="py-filename">doc/core/examples/wxdemo.py</code>.</p>
326
327     <h3>CoreFoundation<a name="auto14"/></h3><a name="cfreactor" shape="rect"/>
328
329     <p>Twisted integrates with <a href="http://pyobjc.sf.net/" shape="rect">PyObjC</a> version 1.0. Sample applications using Cocoa and Twisted
330     are available in the examples directory under
331     <code>doc/core/examples/threadedselect/Cocoa</code>.</p>
332
333 <pre class="python"><p class="py-linenumber">1
334 2
335 3
336 4
337 </p><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>
338 <span class="py-src-variable">cfreactor</span>.<span class="py-src-variable">install</span>()
339
340 <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>
341 </pre>
342
343     <h2>Non-Reactor GUI Integration<a name="auto15"/></h2>
344
345     <h3>Tkinter<a name="auto16"/></h3><a name="tkinter" shape="rect"/>
346
347     <p>The support for <a href="http://wiki.python.org/moin/TkInter" shape="rect">Tkinter</a> doesn't use a specialized reactor.  Instead, there is
348     some specialized support code:</p>
349
350 <pre class="python"><p class="py-linenumber"> 1
351  2
352  3
353  4
354  5
355  6
356  7
357  8
358  9
359 10
360 11
361 </p><span class="py-src-keyword">from</span> <span class="py-src-variable">Tkinter</span> <span class="py-src-keyword">import</span> *
362 <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">reactor</span>
363
364 <span class="py-src-variable">root</span> = <span class="py-src-variable">Tk</span>()
365
366 <span class="py-src-comment"># Install the Reactor support</span>
367 <span class="py-src-variable">tksupport</span>.<span class="py-src-variable">install</span>(<span class="py-src-variable">root</span>)
368
369 <span class="py-src-comment"># at this point build Tk app as usual using the root object,</span>
370 <span class="py-src-comment"># and start the program with &quot;reactor.run()&quot;, and stop it</span>
371 <span class="py-src-comment"># with &quot;reactor.stop()&quot;.</span>
372 </pre>
373
374     <h3>PyUI<a name="auto17"/></h3><a name="pyui" shape="rect"/>
375
376     <p>As with <a href="#tkinter" shape="rect">Tkinter</a>, the support for integrating
377     Twisted with a <a href="http://pyui.sourceforge.net" shape="rect">PyUI</a>
378     application uses specialized support code rather than a simple reactor.</p>
379
380 <pre class="python"><p class="py-linenumber">1
381 2
382 3
383 </p><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>
384
385 <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>})
386 </pre>
387
388     <p>An example Twisted application that uses PyUI can be found in <code class="py-filename">doc/core/examples/pyuidemo.py</code>.</p>
389
390   </div>
391
392     <p><a href="index.html">Index</a></p>
393     <span class="version">Version: 12.1.0</span>
394   </body>
395 </html>