summaryrefslogtreecommitdiff
path: root/doc/modules/timeout.html
blob: bf517ecc24d6991e14c6160885a825ff8e4ae8f0 (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

<!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>timeout – Universal Timeouts &#8212; Eventlet 0.32.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="websocket – Websocket Server" href="websocket.html" />
    <link rel="prev" title="semaphore – Semaphore classes" href="semaphore.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="websocket.html" title="websocket – Websocket Server"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="semaphore.html" title="semaphore – Semaphore classes"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">Eventlet 0.32.0 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../modules.html" accesskey="U">Module Reference</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">timeout</span></code> – Universal Timeouts</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="timeout-universal-timeouts">
<h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">timeout</span></code> – Universal Timeouts<a class="headerlink" href="#timeout-universal-timeouts" title="Permalink to this headline">¶</a></h1>
<dl class="py class">
<dt class="sig sig-object py" id="eventlet.timeout.Timeout">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">eventlet.timeout.</span></span><span class="sig-name descname"><span class="pre">Timeout</span></span><a class="headerlink" href="#eventlet.timeout.Timeout" title="Permalink to this definition">¶</a></dt>
<dd><p>Raises <em>exception</em> in the current greenthread after <em>timeout</em> seconds:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">timeout</span> <span class="o">=</span> <span class="n">Timeout</span><span class="p">(</span><span class="n">seconds</span><span class="p">,</span> <span class="n">exception</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
    <span class="o">...</span> <span class="c1"># execution here is limited by timeout</span>
<span class="k">finally</span><span class="p">:</span>
    <span class="n">timeout</span><span class="o">.</span><span class="n">cancel</span><span class="p">()</span>
</pre></div>
</div>
<p>When <em>exception</em> is omitted or is <code class="docutils literal notranslate"><span class="pre">None</span></code>, the <code class="xref py py-class docutils literal notranslate"><span class="pre">Timeout</span></code> instance
itself is raised:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">Timeout</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">eventlet</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.2</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
 <span class="c">...</span>
<span class="gr">Timeout</span>: <span class="n">0.1 seconds</span>
</pre></div>
</div>
<p>You can use the  <code class="docutils literal notranslate"><span class="pre">with</span></code> statement for additional convenience:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">Timeout</span><span class="p">(</span><span class="n">seconds</span><span class="p">,</span> <span class="n">exception</span><span class="p">)</span> <span class="k">as</span> <span class="n">timeout</span><span class="p">:</span>
    <span class="k">pass</span> <span class="c1"># ... code block ...</span>
</pre></div>
</div>
<p>This is equivalent to the try/finally block in the first example.</p>
<p>There is an additional feature when using the <code class="docutils literal notranslate"><span class="pre">with</span></code> statement: if
<em>exception</em> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, the timeout is still raised, but the with
statement suppresses it, so the code outside the with-block won’t see it:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">data</span> <span class="o">=</span> <span class="kc">None</span>
<span class="k">with</span> <span class="n">Timeout</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="kc">False</span><span class="p">):</span>
    <span class="n">data</span> <span class="o">=</span> <span class="n">mysock</span><span class="o">.</span><span class="n">makefile</span><span class="p">()</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
<span class="k">if</span> <span class="n">data</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
    <span class="o">...</span> <span class="c1"># 5 seconds passed without reading a line</span>
<span class="k">else</span><span class="p">:</span>
    <span class="o">...</span> <span class="c1"># a line was read within 5 seconds</span>
</pre></div>
</div>
<p>As a very special case, if <em>seconds</em> is None, the timer is not scheduled,
and is only useful if you’re planning to raise it directly.</p>
<p>There are two Timeout caveats to be aware of:</p>
<ul class="simple">
<li><p>If the code block in the try/finally or with-block never cooperatively yields, the timeout cannot be raised.  In Eventlet, this should rarely be a problem, but be aware that you cannot time out CPU-only operations with this class.</p></li>
<li><p>If the code block catches and doesn’t re-raise <a class="reference external" href="https://docs.python.org/3/library/exceptions.html#BaseException" title="(in Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseException</span></code></a>  (for example, with <code class="docutils literal notranslate"><span class="pre">except:</span></code>), then it will catch the Timeout exception, and might not abort as intended.</p></li>
</ul>
<p>When catching timeouts, keep in mind that the one you catch may not be the
one you set; if you plan on silencing a timeout, always check that it’s the
same instance that you set:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">timeout</span> <span class="o">=</span> <span class="n">Timeout</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
    <span class="o">...</span>
<span class="k">except</span> <span class="n">Timeout</span> <span class="k">as</span> <span class="n">t</span><span class="p">:</span>
    <span class="k">if</span> <span class="n">t</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">timeout</span><span class="p">:</span>
        <span class="k">raise</span> <span class="c1"># not my timeout</span>
</pre></div>
</div>
<dl class="py method">
<dt class="sig sig-object py" id="eventlet.timeout.eventlet.timeout.Timeout.Timeout.cancel">
<span class="sig-prename descclassname"><span class="pre">Timeout.</span></span><span class="sig-name descname"><span class="pre">cancel</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#eventlet.timeout.eventlet.timeout.Timeout.Timeout.cancel" title="Permalink to this definition">¶</a></dt>
<dd><p>If the timeout is pending, cancel it.  If not using
Timeouts in <code class="docutils literal notranslate"><span class="pre">with</span></code> statements, always call cancel() in a
<code class="docutils literal notranslate"><span class="pre">finally</span></code> after the block of code that is getting timed out.
If not canceled, the timeout will be raised later on, in some
unexpected section of the application.</p>
</dd></dl>

<dl class="py attribute">
<dt class="sig sig-object py" id="eventlet.timeout.eventlet.timeout.Timeout.Timeout.pending">
<span class="sig-prename descclassname"><span class="pre">Timeout.</span></span><span class="sig-name descname"><span class="pre">pending</span></span><a class="headerlink" href="#eventlet.timeout.eventlet.timeout.Timeout.Timeout.pending" title="Permalink to this definition">¶</a></dt>
<dd><p>True if the timeout is scheduled to be raised.</p>
</dd></dl>

</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="eventlet.timeout.with_timeout">
<span class="sig-prename descclassname"><span class="pre">eventlet.timeout.</span></span><span class="sig-name descname"><span class="pre">with_timeout</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">function</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">kwds</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#eventlet.timeout.with_timeout" title="Permalink to this definition">¶</a></dt>
<dd><p>Wrap a call to some (yielding) function with a timeout; if the called
function fails to return before the timeout, cancel it and return a flag
value.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>seconds</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.9)"><em>int</em></a><em> or </em><a class="reference external" href="https://docs.python.org/3/library/functions.html#float" title="(in Python v3.9)"><em>float</em></a>) – seconds before timeout occurs</p></li>
<li><p><strong>func</strong> – the callable to execute with a timeout; it must cooperatively yield, or else the timeout will not be able to trigger</p></li>
<li><p><strong>*args</strong> – positional arguments to pass to <em>func</em></p></li>
<li><p><strong>**kwds</strong> – keyword arguments to pass to <em>func</em></p></li>
<li><p><strong>timeout_value</strong> – value to return if timeout occurs (by default raises
<a class="reference internal" href="#eventlet.timeout.Timeout" title="eventlet.timeout.Timeout"><code class="xref py py-class docutils literal notranslate"><span class="pre">Timeout</span></code></a>)</p></li>
</ul>
</dd>
<dt class="field-even">Return type</dt>
<dd class="field-even"><p>Value returned by <em>func</em> if <em>func</em> returns before <em>seconds</em>, else
<em>timeout_value</em> if provided, else raises <a class="reference internal" href="#eventlet.timeout.Timeout" title="eventlet.timeout.Timeout"><code class="xref py py-class docutils literal notranslate"><span class="pre">Timeout</span></code></a>.</p>
</dd>
<dt class="field-odd">Raises</dt>
<dd class="field-odd"><p><a class="reference internal" href="../basic_usage.html#eventlet.Timeout" title="eventlet.Timeout"><strong>Timeout</strong></a> – if <em>func</em> times out and no <code class="docutils literal notranslate"><span class="pre">timeout_value</span></code> has
been provided.</p>
</dd>
<dt class="field-even">Exception</dt>
<dd class="field-even"><p>Any exception raised by <em>func</em></p>
</dd>
</dl>
<p>Example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">data</span> <span class="o">=</span> <span class="n">with_timeout</span><span class="p">(</span><span class="mi">30</span><span class="p">,</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">open</span><span class="p">,</span> <span class="s1">&#39;http://www.google.com/&#39;</span><span class="p">,</span> <span class="n">timeout_value</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>Here <em>data</em> is either the result of the <code class="docutils literal notranslate"><span class="pre">get()</span></code> call, or the empty string
if it took too long to return.  Any exception raised by the <code class="docutils literal notranslate"><span class="pre">get()</span></code> call
is passed through to the caller.</p>
</dd></dl>

</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="semaphore.html"
                        title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">semaphore</span></code> – Semaphore classes</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="websocket.html"
                        title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">websocket</span></code> – Websocket Server</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/modules/timeout.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="websocket.html" title="websocket – Websocket Server"
             >next</a> |</li>
        <li class="right" >
          <a href="semaphore.html" title="semaphore – Semaphore classes"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">Eventlet 0.32.0 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../modules.html" >Module Reference</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">timeout</span></code> – Universal Timeouts</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>