/usr/share/doc/python-eventlet/html/basic_usage.html is in python-eventlet 0.13.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | <!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Basic Usage — Eventlet 0.13.0 documentation</title>
<link rel="stylesheet" href="_static/default.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '0.13.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="Eventlet 0.13.0 documentation" href="index.html" />
<link rel="next" title="Design Patterns" href="design_patterns.html" />
<link rel="prev" title="Eventlet Documentation" href="index.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="design_patterns.html" title="Design Patterns"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="index.html" title="Eventlet Documentation"
accesskey="P">previous</a> |</li>
<li><a href="index.html">Eventlet 0.13.0 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="basic-usage">
<h1>Basic Usage<a class="headerlink" href="#basic-usage" title="Permalink to this headline">¶</a></h1>
<p>If it’s your first time to Eventlet, you may find the illuminated examples in the <a class="reference internal" href="design_patterns.html#design-patterns"><em>Design Patterns</em></a> document to be a good starting point.</p>
<p>Eventlet is built around the concept of green threads (i.e. coroutines, we use the terms interchangeably) that are launched to do network-related work. Green threads differ from normal threads in two main ways:</p>
<ul class="simple">
<li>Green threads are so cheap they are nearly free. You do not have to conserve green threads like you would normal threads. In general, there will be at least one green thread per network connection.</li>
<li>Green threads cooperatively yield to each other instead of preemptively being scheduled. The major advantage from this behavior is that shared data structures don’t need locks, because only if a yield is explicitly called can another green thread have access to the data structure. It is also possible to inspect primitives such as queues to see if they have any pending data.</li>
</ul>
</div>
<div class="section" id="primary-api">
<h1>Primary API<a class="headerlink" href="#primary-api" title="Permalink to this headline">¶</a></h1>
<p>The design goal for Eventlet’s API is simplicity and readability. You should be able to read its code and understand what it’s doing. Fewer lines of code are preferred over excessively clever implementations. <a class="reference external" href="http://www.python.org/dev/peps/pep-0020/">Like Python itself</a>, there should be one, and only one obvious way to do it in Eventlet!</p>
<p>Though Eventlet has many modules, much of the most-used stuff is accessible simply by doing <tt class="docutils literal"><span class="pre">import</span> <span class="pre">eventlet</span></tt>. Here’s a quick summary of the functionality available in the <tt class="docutils literal"><span class="pre">eventlet</span></tt> module, with links to more verbose documentation on each.</p>
<div class="section" id="greenthread-spawn">
<h2>Greenthread Spawn<a class="headerlink" href="#greenthread-spawn" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="eventlet.spawn">
<tt class="descclassname">eventlet.</tt><tt class="descname">spawn</tt><big>(</big><em>func</em>, <em>*args</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#eventlet.spawn" title="Permalink to this definition">¶</a></dt>
<dd><p>This launches a greenthread to call <em>func</em>. Spawning off multiple greenthreads gets work done in parallel. The return value from <tt class="docutils literal"><span class="pre">spawn</span></tt> is a <tt class="xref py py-class docutils literal"><span class="pre">greenthread.GreenThread</span></tt> object, which can be used to retrieve the return value of <em>func</em>. See <a class="reference internal" href="modules/greenthread.html#eventlet.greenthread.spawn" title="eventlet.greenthread.spawn"><tt class="xref py py-func docutils literal"><span class="pre">spawn</span></tt></a> for more details.</p>
</dd></dl>
<dl class="function">
<dt id="eventlet.spawn_n">
<tt class="descclassname">eventlet.</tt><tt class="descname">spawn_n</tt><big>(</big><em>func</em>, <em>*args</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#eventlet.spawn_n" title="Permalink to this definition">¶</a></dt>
<dd><p>The same as <tt class="xref py py-func docutils literal"><span class="pre">spawn()</span></tt>, but it’s not possible to know how the function terminated (i.e. no return value or exceptions). This makes execution faster. See <a class="reference internal" href="modules/greenthread.html#eventlet.greenthread.spawn_n" title="eventlet.greenthread.spawn_n"><tt class="xref py py-func docutils literal"><span class="pre">spawn_n</span></tt></a> for more details.</p>
</dd></dl>
<dl class="function">
<dt id="eventlet.spawn_after">
<tt class="descclassname">eventlet.</tt><tt class="descname">spawn_after</tt><big>(</big><em>seconds</em>, <em>func</em>, <em>*args</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#eventlet.spawn_after" title="Permalink to this definition">¶</a></dt>
<dd><p>Spawns <em>func</em> after <em>seconds</em> have elapsed; a delayed version of <tt class="xref py py-func docutils literal"><span class="pre">spawn()</span></tt>. To abort the spawn and prevent <em>func</em> from being called, call <tt class="xref py py-meth docutils literal"><span class="pre">GreenThread.cancel()</span></tt> on the return value of <tt class="xref py py-func docutils literal"><span class="pre">spawn_after()</span></tt>. See <a class="reference internal" href="modules/greenthread.html#eventlet.greenthread.spawn_after" title="eventlet.greenthread.spawn_after"><tt class="xref py py-func docutils literal"><span class="pre">spawn_after</span></tt></a> for more details.</p>
</dd></dl>
</div>
<div class="section" id="greenthread-control">
<h2>Greenthread Control<a class="headerlink" href="#greenthread-control" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="eventlet.sleep">
<tt class="descclassname">eventlet.</tt><tt class="descname">sleep</tt><big>(</big><em>seconds=0</em><big>)</big><a class="headerlink" href="#eventlet.sleep" title="Permalink to this definition">¶</a></dt>
<dd><p>Suspends the current greenthread and allows others a chance to process. See <a class="reference internal" href="modules/greenthread.html#eventlet.greenthread.sleep" title="eventlet.greenthread.sleep"><tt class="xref py py-func docutils literal"><span class="pre">sleep</span></tt></a> for more details.</p>
</dd></dl>
<dl class="class">
<dt id="eventlet.GreenPool">
<em class="property">class </em><tt class="descclassname">eventlet.</tt><tt class="descname">GreenPool</tt><a class="headerlink" href="#eventlet.GreenPool" title="Permalink to this definition">¶</a></dt>
<dd><p>Pools control concurrency. It’s very common in applications to want to consume only a finite amount of memory, or to restrict the amount of connections that one part of the code holds open so as to leave more for the rest, or to behave consistently in the face of unpredictable input data. GreenPools provide this control. See <a class="reference internal" href="modules/greenpool.html#eventlet.greenpool.GreenPool" title="eventlet.greenpool.GreenPool"><tt class="xref py py-class docutils literal"><span class="pre">GreenPool</span></tt></a> for more on how to use these.</p>
</dd></dl>
<dl class="class">
<dt id="eventlet.GreenPile">
<em class="property">class </em><tt class="descclassname">eventlet.</tt><tt class="descname">GreenPile</tt><a class="headerlink" href="#eventlet.GreenPile" title="Permalink to this definition">¶</a></dt>
<dd><p>GreenPile objects represent chunks of work. In essence a GreenPile is an iterator that can be stuffed with work, and the results read out later. See <a class="reference internal" href="modules/greenpool.html#eventlet.greenpool.GreenPile" title="eventlet.greenpool.GreenPile"><tt class="xref py py-class docutils literal"><span class="pre">GreenPile</span></tt></a> for more details.</p>
</dd></dl>
<dl class="class">
<dt id="eventlet.Queue">
<em class="property">class </em><tt class="descclassname">eventlet.</tt><tt class="descname">Queue</tt><a class="headerlink" href="#eventlet.Queue" title="Permalink to this definition">¶</a></dt>
<dd><p>Queues are a fundamental construct for communicating data between execution units. Eventlet’s Queue class is used to communicate between greenthreads, and provides a bunch of useful features for doing that. See <a class="reference internal" href="modules/queue.html#eventlet.queue.Queue" title="eventlet.queue.Queue"><tt class="xref py py-class docutils literal"><span class="pre">Queue</span></tt></a> for more details.</p>
</dd></dl>
<dl class="class">
<dt id="eventlet.Timeout">
<em class="property">class </em><tt class="descclassname">eventlet.</tt><tt class="descname">Timeout</tt><a class="headerlink" href="#eventlet.Timeout" title="Permalink to this definition">¶</a></dt>
<dd><p>This class is a way to add timeouts to anything. It raises <em>exception</em> in the current greenthread after <em>timeout</em> seconds. When <em>exception</em> is omitted or <tt class="docutils literal"><span class="pre">None</span></tt>, the Timeout instance itself is raised.</p>
<p>Timeout objects are context managers, and so can be used in with statements.
See <a class="reference internal" href="modules/timeout.html#eventlet.timeout.Timeout" title="eventlet.timeout.Timeout"><tt class="xref py py-class docutils literal"><span class="pre">Timeout</span></tt></a> for more details.</p>
</dd></dl>
</div>
<div class="section" id="patching-functions">
<h2>Patching Functions<a class="headerlink" href="#patching-functions" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="eventlet.import_patched">
<tt class="descclassname">eventlet.</tt><tt class="descname">import_patched</tt><big>(</big><em>modulename</em>, <em>*additional_modules</em>, <em>**kw_additional_modules</em><big>)</big><a class="headerlink" href="#eventlet.import_patched" title="Permalink to this definition">¶</a></dt>
<dd><p>Imports a module in a way that ensures that the module uses “green” versions of the standard library modules, so that everything works nonblockingly. The only required argument is the name of the module to be imported. For more information see <a class="reference internal" href="patching.html#import-green"><em>Import Green</em></a>.</p>
</dd></dl>
<dl class="function">
<dt id="eventlet.monkey_patch">
<tt class="descclassname">eventlet.</tt><tt class="descname">monkey_patch</tt><big>(</big><em>all=True</em>, <em>os=False</em>, <em>select=False</em>, <em>socket=False</em>, <em>thread=False</em>, <em>time=False</em><big>)</big><a class="headerlink" href="#eventlet.monkey_patch" title="Permalink to this definition">¶</a></dt>
<dd><p>Globally patches certain system modules to be greenthread-friendly. The keyword arguments afford some control over which modules are patched. If <em>all</em> is True, then all modules are patched regardless of the other arguments. If it’s False, then the rest of the keyword arguments control patching of specific subsections of the standard library. Most patch the single module of the same name (os, time, select). The exceptions are socket, which also patches the ssl module if present; and thread, which patches thread, threading, and Queue. It’s safe to call monkey_patch multiple times. For more information see <a class="reference internal" href="patching.html#monkey-patch"><em>Monkeypatching the Standard Library</em></a>.</p>
</dd></dl>
</div>
<div class="section" id="network-convenience-functions">
<h2>Network Convenience Functions<a class="headerlink" href="#network-convenience-functions" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="eventlet.connect">
<tt class="descclassname">eventlet.</tt><tt class="descname">connect</tt><big>(</big><em>addr</em>, <em>family=2</em>, <em>bind=None</em><big>)</big><a class="headerlink" href="#eventlet.connect" title="Permalink to this definition">¶</a></dt>
<dd><p>Convenience function for opening client sockets.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>addr</strong> – Address of the server to connect to. For TCP sockets, this is a (host, port) tuple.</li>
<li><strong>family</strong> – Socket family, optional. See <tt class="xref py py-mod docutils literal"><span class="pre">socket</span></tt> documentation for available families.</li>
<li><strong>bind</strong> – Local address to bind to, optional.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">The connected green socket object.</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="eventlet.listen">
<tt class="descclassname">eventlet.</tt><tt class="descname">listen</tt><big>(</big><em>addr</em>, <em>family=2</em>, <em>backlog=50</em><big>)</big><a class="headerlink" href="#eventlet.listen" title="Permalink to this definition">¶</a></dt>
<dd><p>Convenience function for opening server sockets. This
socket can be used in <a class="reference internal" href="#eventlet.serve" title="eventlet.serve"><tt class="xref py py-func docutils literal"><span class="pre">serve()</span></tt></a> or a custom <tt class="docutils literal"><span class="pre">accept()</span></tt> loop.</p>
<p>Sets SO_REUSEADDR on the socket to save on annoyance.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>addr</strong> – Address to listen on. For TCP sockets, this is a (host, port) tuple.</li>
<li><strong>family</strong> – Socket family, optional. See <tt class="xref py py-mod docutils literal"><span class="pre">socket</span></tt> documentation for available families.</li>
<li><strong>backlog</strong> – The maximum number of queued connections. Should be at least 1; the maximum value is system-dependent.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">The listening green socket object.</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="eventlet.wrap_ssl">
<tt class="descclassname">eventlet.</tt><tt class="descname">wrap_ssl</tt><big>(</big><em>sock</em>, <em>*a</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#eventlet.wrap_ssl" title="Permalink to this definition">¶</a></dt>
<dd><p>Convenience function for converting a regular socket into an
SSL socket. Has the same interface as <tt class="xref py py-func docutils literal"><span class="pre">ssl.wrap_socket()</span></tt>,
but works on 2.5 or earlier, using PyOpenSSL (though note that it
ignores the <em>cert_reqs</em>, <em>ssl_version</em>, <em>ca_certs</em>,
<em>do_handshake_on_connect</em>, and <em>suppress_ragged_eofs</em> arguments
when using PyOpenSSL).</p>
<p>The preferred idiom is to call wrap_ssl directly on the creation
method, e.g., <tt class="docutils literal"><span class="pre">wrap_ssl(connect(addr))</span></tt> or
<tt class="docutils literal"><span class="pre">wrap_ssl(listen(addr),</span> <span class="pre">server_side=True)</span></tt>. This way there is
no “naked” socket sitting around to accidentally corrupt the SSL
session.</p>
<p>:return Green SSL object.</p>
</dd></dl>
<dl class="function">
<dt id="eventlet.serve">
<tt class="descclassname">eventlet.</tt><tt class="descname">serve</tt><big>(</big><em>sock</em>, <em>handle</em>, <em>concurrency=1000</em><big>)</big><a class="headerlink" href="#eventlet.serve" title="Permalink to this definition">¶</a></dt>
<dd><p>Runs a server on the supplied socket. Calls the function <em>handle</em> in a
separate greenthread for every incoming client connection. <em>handle</em> takes
two arguments: the client socket object, and the client address:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">myhandle</span><span class="p">(</span><span class="n">client_sock</span><span class="p">,</span> <span class="n">client_addr</span><span class="p">):</span>
<span class="k">print</span> <span class="s">"client connected"</span><span class="p">,</span> <span class="n">client_addr</span>
<span class="n">eventlet</span><span class="o">.</span><span class="n">serve</span><span class="p">(</span><span class="n">eventlet</span><span class="o">.</span><span class="n">listen</span><span class="p">((</span><span class="s">'127.0.0.1'</span><span class="p">,</span> <span class="mi">9999</span><span class="p">)),</span> <span class="n">myhandle</span><span class="p">)</span>
</pre></div>
</div>
<p>Returning from <em>handle</em> closes the client socket.</p>
<p><tt class="xref py py-func docutils literal"><span class="pre">serve()</span></tt> blocks the calling greenthread; it won’t return until
the server completes. If you desire an immediate return,
spawn a new greenthread for <tt class="xref py py-func docutils literal"><span class="pre">serve()</span></tt>.</p>
<p>Any uncaught exceptions raised in <em>handle</em> are raised as exceptions
from <tt class="xref py py-func docutils literal"><span class="pre">serve()</span></tt>, terminating the server, so be sure to be aware of the
exceptions your application can raise. The return value of <em>handle</em> is
ignored.</p>
<p>Raise a <a class="reference internal" href="#eventlet.StopServe" title="eventlet.StopServe"><tt class="xref py py-class docutils literal"><span class="pre">StopServe</span></tt></a> exception to gracefully terminate the
server – that’s the only way to get the server() function to return rather
than raise.</p>
<p>The value in <em>concurrency</em> controls the maximum number of
greenthreads that will be open at any time handling requests. When
the server hits the concurrency limit, it stops accepting new
connections until the existing ones complete.</p>
</dd></dl>
<dl class="class">
<dt id="eventlet.StopServe">
<em class="property">class </em><tt class="descclassname">eventlet.</tt><tt class="descname">StopServe</tt><a class="headerlink" href="#eventlet.StopServe" title="Permalink to this definition">¶</a></dt>
<dd><p>Exception class used for quitting <a class="reference internal" href="#eventlet.serve" title="eventlet.serve"><tt class="xref py py-func docutils literal"><span class="pre">serve()</span></tt></a> gracefully.</p>
</dd></dl>
<p>These are the basic primitives of Eventlet; there are a lot more out there in the other Eventlet modules; check out the <a class="reference internal" href="modules.html"><em>Module Reference</em></a>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Basic Usage</a></li>
<li><a class="reference internal" href="#primary-api">Primary API</a><ul>
<li><a class="reference internal" href="#greenthread-spawn">Greenthread Spawn</a></li>
<li><a class="reference internal" href="#greenthread-control">Greenthread Control</a></li>
<li><a class="reference internal" href="#patching-functions">Patching Functions</a></li>
<li><a class="reference internal" href="#network-convenience-functions">Network Convenience Functions</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="index.html"
title="previous chapter">Eventlet Documentation</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="design_patterns.html"
title="next chapter">Design Patterns</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/basic_usage.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="design_patterns.html" title="Design Patterns"
>next</a> |</li>
<li class="right" >
<a href="index.html" title="Eventlet Documentation"
>previous</a> |</li>
<li><a href="index.html">Eventlet 0.13.0 documentation</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2005-2010, Eventlet Contributors.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.3.
</div>
</body>
</html>
|