summaryrefslogtreecommitdiff
path: root/doc/basic_usage.html
blob: adc0903f83225a8ef224adf75f20ed19c76a73e0 (plain)
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

<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>Basic Usage &#8212; Eventlet 0.33.0 documentation</title>
    <link rel="stylesheet" type="text/css" href="_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="_static/classic.css" />
    
    <script data-url_root="./" id="documentation_options" src="_static/documentation_options.js"></script>
    <script src="_static/jquery.js"></script>
    <script src="_static/underscore.js"></script>
    <script src="_static/doctools.js"></script>
    
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.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" role="navigation" aria-label="related navigation">
      <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 class="nav-item nav-item-0"><a href="index.html">Eventlet 0.33.0 documentation</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">Basic Usage</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <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"><span class="std std-ref">Design Patterns</span></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><p>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.</p></li>
<li><p>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.</p></li>
</ul>
</section>
<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 <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">eventlet</span></code>.  Here’s a quick summary of the functionality available in the <code class="docutils literal notranslate"><span class="pre">eventlet</span></code> module, with links to more verbose documentation on each.</p>
<section id="greenthread-spawn">
<h2>Greenthread Spawn<a class="headerlink" href="#greenthread-spawn" title="Permalink to this headline">¶</a></h2>
<dl class="py function">
<dt class="sig sig-object py" id="eventlet.spawn">
<span class="sig-prename descclassname"><span class="pre">eventlet.</span></span><span class="sig-name descname"><span class="pre">spawn</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kw</span></span></em><span class="sig-paren">)</span><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 <code class="docutils literal notranslate"><span class="pre">spawn</span></code> is a <a class="reference internal" href="modules/greenthread.html#eventlet.greenthread.GreenThread" title="eventlet.greenthread.GreenThread"><code class="xref py py-class docutils literal notranslate"><span class="pre">greenthread.GreenThread</span></code></a> 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"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawn</span></code></a> for more details.</p>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="eventlet.spawn_n">
<span class="sig-prename descclassname"><span class="pre">eventlet.</span></span><span class="sig-name descname"><span class="pre">spawn_n</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kw</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#eventlet.spawn_n" title="Permalink to this definition">¶</a></dt>
<dd><p>The same as <a class="reference internal" href="#eventlet.spawn" title="eventlet.spawn"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawn()</span></code></a>, 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"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawn_n</span></code></a> for more details.</p>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="eventlet.spawn_after">
<span class="sig-prename descclassname"><span class="pre">eventlet.</span></span><span class="sig-name descname"><span class="pre">spawn_after</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">seconds</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">func</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kw</span></span></em><span class="sig-paren">)</span><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 <a class="reference internal" href="#eventlet.spawn" title="eventlet.spawn"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawn()</span></code></a>.   To abort the spawn and prevent <em>func</em> from being called, call <code class="xref py py-meth docutils literal notranslate"><span class="pre">GreenThread.cancel()</span></code> on the return value of <a class="reference internal" href="#eventlet.spawn_after" title="eventlet.spawn_after"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawn_after()</span></code></a>.  See <a class="reference internal" href="modules/greenthread.html#eventlet.greenthread.spawn_after" title="eventlet.greenthread.spawn_after"><code class="xref py py-func docutils literal notranslate"><span class="pre">spawn_after</span></code></a> for more details.</p>
</dd></dl>

</section>
<section id="greenthread-control">
<h2>Greenthread Control<a class="headerlink" href="#greenthread-control" title="Permalink to this headline">¶</a></h2>
<dl class="py function">
<dt class="sig sig-object py" id="eventlet.sleep">
<span class="sig-prename descclassname"><span class="pre">eventlet.</span></span><span class="sig-name descname"><span class="pre">sleep</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">seconds</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><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"><code class="xref py py-func docutils literal notranslate"><span class="pre">sleep</span></code></a> for more details.</p>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="eventlet.GreenPool">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">eventlet.</span></span><span class="sig-name descname"><span class="pre">GreenPool</span></span><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"><code class="xref py py-class docutils literal notranslate"><span class="pre">GreenPool</span></code></a> for more on how to use these.</p>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="eventlet.GreenPile">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">eventlet.</span></span><span class="sig-name descname"><span class="pre">GreenPile</span></span><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"><code class="xref py py-class docutils literal notranslate"><span class="pre">GreenPile</span></code></a> for more details.</p>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="eventlet.Queue">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">eventlet.</span></span><span class="sig-name descname"><span class="pre">Queue</span></span><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"><code class="xref py py-class docutils literal notranslate"><span class="pre">Queue</span></code></a> for more details.</p>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="eventlet.Timeout">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">eventlet.</span></span><span class="sig-name descname"><span class="pre">Timeout</span></span><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 <code class="docutils literal notranslate"><span class="pre">None</span></code>, 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"><code class="xref py py-class docutils literal notranslate"><span class="pre">Timeout</span></code></a> for more details.</p>
</dd></dl>

</section>
<section id="patching-functions">
<h2>Patching Functions<a class="headerlink" href="#patching-functions" title="Permalink to this headline">¶</a></h2>
<dl class="py function">
<dt class="sig sig-object py" id="eventlet.import_patched">
<span class="sig-prename descclassname"><span class="pre">eventlet.</span></span><span class="sig-name descname"><span class="pre">import_patched</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">modulename</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">additional_modules</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kw_additional_modules</span></span></em><span class="sig-paren">)</span><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"><span class="std std-ref">Import Green</span></a>.</p>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="eventlet.monkey_patch">
<span class="sig-prename descclassname"><span class="pre">eventlet.</span></span><span class="sig-name descname"><span class="pre">monkey_patch</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">all</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">os</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">select</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">socket</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">thread</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">time</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><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"><span class="std std-ref">Monkeypatching the Standard Library</span></a>.</p>
</dd></dl>

</section>
<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="py function">
<dt class="sig sig-object py" id="eventlet.connect">
<span class="sig-prename descclassname"><span class="pre">eventlet.</span></span><span class="sig-name descname"><span class="pre">connect</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">addr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">family</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">AddressFamily.AF_INET</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">bind</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#eventlet.connect" title="Permalink to this definition">¶</a></dt>
<dd><p>Convenience function for opening client sockets.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>addr</strong> – Address of the server to connect to.  For TCP sockets, this is a (host, port) tuple.</p></li>
<li><p><strong>family</strong> – Socket family, optional.  See <a class="reference external" href="https://docs.python.org/3/library/socket.html#module-socket" title="(in Python v3.10)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">socket</span></code></a> documentation for available families.</p></li>
<li><p><strong>bind</strong> – Local address to bind to, optional.</p></li>
</ul>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>The connected green socket object.</p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="eventlet.listen">
<span class="sig-prename descclassname"><span class="pre">eventlet.</span></span><span class="sig-name descname"><span class="pre">listen</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">addr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">family</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">AddressFamily.AF_INET</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">backlog</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">50</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">reuse_addr</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">reuse_port</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><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"><code class="xref py py-func docutils literal notranslate"><span class="pre">serve()</span></code></a> or a custom <code class="docutils literal notranslate"><span class="pre">accept()</span></code> loop.</p>
<p>Sets SO_REUSEADDR on the socket to save on annoyance.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>addr</strong> – Address to listen on.  For TCP sockets, this is a (host, port)  tuple.</p></li>
<li><p><strong>family</strong> – Socket family, optional.  See <a class="reference external" href="https://docs.python.org/3/library/socket.html#module-socket" title="(in Python v3.10)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">socket</span></code></a> documentation for available families.</p></li>
<li><p><strong>backlog</strong> – The maximum number of queued connections. Should be at least 1; the maximum
value is system-dependent.</p></li>
</ul>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p>The listening green socket object.</p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="eventlet.wrap_ssl">
<span class="sig-prename descclassname"><span class="pre">eventlet.</span></span><span class="sig-name descname"><span class="pre">wrap_ssl</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sock</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">a</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kw</span></span></em><span class="sig-paren">)</span><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 <a class="reference external" href="https://docs.python.org/3/library/ssl.html#ssl.wrap_socket" title="(in Python v3.10)"><code class="xref py py-func docutils literal notranslate"><span class="pre">ssl.wrap_socket()</span></code></a>,
but can also use PyOpenSSL. Though, note that it ignores the
<cite>cert_reqs</cite>, <cite>ssl_version</cite>, <cite>ca_certs</cite>, <cite>do_handshake_on_connect</cite>,
and <cite>suppress_ragged_eofs</cite> arguments when using PyOpenSSL.</p>
<p>The preferred idiom is to call wrap_ssl directly on the creation
method, e.g., <code class="docutils literal notranslate"><span class="pre">wrap_ssl(connect(addr))</span></code> or
<code class="docutils literal notranslate"><span class="pre">wrap_ssl(listen(addr),</span> <span class="pre">server_side=True)</span></code>. 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="py function">
<dt class="sig sig-object py" id="eventlet.serve">
<span class="sig-prename descclassname"><span class="pre">eventlet.</span></span><span class="sig-name descname"><span class="pre">serve</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">sock</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">handle</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">concurrency</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1000</span></span></em><span class="sig-paren">)</span><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-default notranslate"><div class="highlight"><pre><span></span><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="nb">print</span><span class="p">(</span><span class="s2">&quot;client connected&quot;</span><span class="p">,</span> <span class="n">client_addr</span><span class="p">)</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="s1">&#39;127.0.0.1&#39;</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><a class="reference internal" href="#eventlet.serve" title="eventlet.serve"><code class="xref py py-func docutils literal notranslate"><span class="pre">serve()</span></code></a> blocks the calling greenthread; it won’t return until
the server completes.  If you desire an immediate return,
spawn a new greenthread for <a class="reference internal" href="#eventlet.serve" title="eventlet.serve"><code class="xref py py-func docutils literal notranslate"><span class="pre">serve()</span></code></a>.</p>
<p>Any uncaught exceptions raised in <em>handle</em> are raised as exceptions
from <a class="reference internal" href="#eventlet.serve" title="eventlet.serve"><code class="xref py py-func docutils literal notranslate"><span class="pre">serve()</span></code></a>, 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"><code class="xref py py-class docutils literal notranslate"><span class="pre">StopServe</span></code></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="py class">
<dt class="sig sig-object py" id="eventlet.StopServe">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">eventlet.</span></span><span class="sig-name descname"><span class="pre">StopServe</span></span><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"><code class="xref py py-func docutils literal notranslate"><span class="pre">serve()</span></code></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"><span class="doc">Module Reference</span></a>.</p>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <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>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="_sources/basic_usage.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <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 class="nav-item nav-item-0"><a href="index.html">Eventlet 0.33.0 documentation</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">Basic Usage</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
        &#169; Copyright 2005-2010, Eventlet Contributors.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.1.2.
    </div>
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-42952223-1', 'eventlet.net');
  ga('send', 'pageview');
</script>

  </body>
</html>