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">
3 <title>Twisted Documentation: Reactor Overview</title>
4 <link href="stylesheet.css" rel="stylesheet" type="text/css"/>
8 <h1 class="title">Reactor Overview</h1>
9 <div class="toc"><ol><li><a href="#auto0">Reactor Basics</a></li><li><a href="#auto1">Using the reactor object</a></li></ol></div>
15 This HOWTO introduces the Twisted reactor, describes the basics of the
16 reactor and links to the various reactor interfaces.
19 <h2>Reactor Basics<a name="auto0"/></h2>
21 <p>The reactor is the core of the event loop within Twisted -- the loop
22 which drives applications using Twisted. The event loop is a programming
23 construct that waits for and dispatches events or messages in a program.
24 It works by calling some internal or external "event provider", which
25 generally blocks until an event has arrived, and then calls the relevant
26 event handler ("dispatches the event"). The reactor provides basic
27 interfaces to a number of services, including network communications,
28 threading, and event dispatching.
32 For information about using the reactor and the Twisted event loop, see:
36 <li>the event dispatching howtos: <a href="time.html" shape="rect">Scheduling</a> and <a href="defer.html" shape="rect">Using Deferreds</a>;</li>
37 <li>the communication howtos: <a href="servers.html" shape="rect">TCP
38 servers</a>, <a href="clients.html" shape="rect">TCP clients</a>, <a href="udp.html" shape="rect">UDP networking</a> and <a href="process.html" shape="rect">Using
39 processes</a>; and</li>
40 <li><a href="threading.html" shape="rect">Using threads</a>.</li>
43 <p>There are multiple implementations of the reactor, each
44 modified to provide better support for specialized features
45 over the default implementation. More information about these
46 and how to use a particular implementation is available via
47 <a href="choosing-reactor.html" shape="rect">Choosing a Reactor</a>.</p>
51 Twisted applications can use the interfaces in <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.application.service.html" title="twisted.application.service">twisted.application.service</a></code> to configure and run the
52 application instead of using
53 boilerplate reactor code. See <a href="application.html" shape="rect">Using Application</a> for an introduction to
57 <h2>Using the reactor object<a name="auto1"/></h2>
59 <p>You can get to the <code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.internet.reactor.html" title="twisted.internet.reactor">reactor</a></code> object using the following code:</p>
61 <pre class="python"><p class="py-linenumber">1
62 </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>
65 <p>The reactor usually implements a set of interfaces, but
66 depending on the chosen reactor and the platform, some of
67 the interfaces may not be implemented:</p>
70 <li><code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.internet.interfaces.IReactorCore.html" title="twisted.internet.interfaces.IReactorCore">IReactorCore</a></code>: Core (required) functionality.</li>
71 <li><code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.internet.interfaces.IReactorFDSet.html" title="twisted.internet.interfaces.IReactorFDSet">IReactorFDSet</a></code>: Use FileDescriptor objects.</li>
72 <li><code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.internet.interfaces.IReactorProcess.html" title="twisted.internet.interfaces.IReactorProcess">IReactorProcess</a></code>: Process management. Read the
73 <a href="process.html" shape="rect">Using Processes</a> document for
74 more information.</li>
75 <li><code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.internet.interfaces.IReactorSSL.html" title="twisted.internet.interfaces.IReactorSSL">IReactorSSL</a></code>: SSL networking support.</li>
76 <li><code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.internet.interfaces.IReactorTCP.html" title="twisted.internet.interfaces.IReactorTCP">IReactorTCP</a></code>: TCP networking support. More information
77 can be found in the <a href="servers.html" shape="rect">Writing Servers</a>
78 and <a href="clients.html" shape="rect">Writing Clients</a> documents.</li>
79 <li><code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.internet.interfaces.IReactorThreads.html" title="twisted.internet.interfaces.IReactorThreads">IReactorThreads</a></code>: Threading use and management. More
80 information can be found within <a href="threading.html" shape="rect">Threading In Twisted</a>.</li>
81 <li><code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.internet.interfaces.IReactorTime.html" title="twisted.internet.interfaces.IReactorTime">IReactorTime</a></code>: Scheduling interface. More information
82 can be found within <a href="time.html" shape="rect">Scheduling Tasks</a>.</li>
83 <li><code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.internet.interfaces.IReactorUDP.html" title="twisted.internet.interfaces.IReactorUDP">IReactorUDP</a></code>: UDP networking support. More information
84 can be found within <a href="udp.html" shape="rect">UDP Networking</a>.</li>
85 <li><code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.internet.interfaces.IReactorUNIX.html" title="twisted.internet.interfaces.IReactorUNIX">IReactorUNIX</a></code>: UNIX socket support.</li>
86 <li><code class="API"><a href="http://twistedmatrix.com/documents/12.1.0/api/twisted.internet.interfaces.IReactorSocket.html" title="twisted.internet.interfaces.IReactorSocket">IReactorSocket</a></code>: Third-party socket support.</li>
90 <p><a href="index.html">Index</a></p>
91 <span class="version">Version: 12.1.0</span>