Sophie

Sophie

distrib > Fedora > 13 > i386 > by-pkgid > 552d72b401c5b4a5a4c52922e7b31f2c > files > 87

python-eventlet-doc-0.9.12-1.fc13.noarch.rpm

<!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>timeout – Universal Timeouts &mdash; Eventlet v0.9.12 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.9.12',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="top" title="Eventlet v0.9.12 documentation" href="../index.html" />
    <link rel="up" title="Module Reference" href="../modules.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">
      <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="../modindex.html" title="Global Module Index"
             accesskey="M">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><a href="../index.html">Eventlet v0.9.12 documentation</a> &raquo;</li>
          <li><a href="../modules.html" accesskey="U">Module Reference</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="timeout-universal-timeouts">
<h1><tt class="xref docutils literal"><span class="pre">timeout</span></tt> &#8211; Universal Timeouts<a class="headerlink" href="#timeout-universal-timeouts" title="Permalink to this headline">¶</a></h1>
<dl class="class">
<dt id="eventlet.timeout.Timeout">
<em class="property">class </em><tt class="descclassname">eventlet.timeout.</tt><tt class="descname">Timeout</tt><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-python"><div class="highlight"><pre><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="c"># 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 <tt class="xref docutils literal"><span class="pre">None</span></tt>, the <tt class="xref docutils literal"><span class="pre">Timeout</span></tt> instance
itself is raised:</p>
<div class="highlight-python"><div class="highlight"><pre><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="nc">Timeout</span>: <span class="n-Identifier">0.1 seconds</span>
</pre></div>
</div>
<p>In Python 2.5 and newer, you can use the  <tt class="docutils literal"><span class="pre">with</span></tt> statement for additional
convenience:</p>
<div class="highlight-python"><div class="highlight"><pre><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="c"># ... 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 <tt class="docutils literal"><span class="pre">with</span></tt> statement: if
<em>exception</em> is <tt class="xref docutils literal"><span class="pre">False</span></tt>, the timeout is still raised, but the with
statement suppresses it, so the code outside the with-block won&#8217;t see it:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">data</span> <span class="o">=</span> <span class="bp">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="bp">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="bp">None</span><span class="p">:</span>
    <span class="o">...</span> <span class="c"># 5 seconds passed without reading a line</span>
<span class="k">else</span><span class="p">:</span>
    <span class="o">...</span> <span class="c"># 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&#8217;re planning to raise it directly.</p>
<p>There are two Timeout caveats to be aware of:</p>
<ul class="simple">
<li>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.</li>
<li>If the code block catches and doesn&#8217;t re-raise <tt class="xref docutils literal"><span class="pre">BaseException</span></tt>  (for example, with <tt class="docutils literal"><span class="pre">except:</span></tt>), then it will catch the Timeout exception, and might not abort as intended.</li>
</ul>
<p>When catching timeouts, keep in mind that the one you catch may not be the
one you have set; if you going to silence a timeout, always check that it&#8217;s
the same instance that you set:</p>
<div class="highlight-python"><div class="highlight"><pre><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="p">,</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="c"># not my timeout</span>
</pre></div>
</div>
<dl class="method">
<dt id="eventlet.timeout.eventlet.timeout.Timeout.Timeout.cancel">
<tt class="descclassname">Timeout.</tt><tt class="descname">cancel</tt><big>(</big><big>)</big><a class="headerlink" href="#eventlet.timeout.eventlet.timeout.Timeout.Timeout.cancel" title="Permalink to this definition">¶</a></dt>
<dd>If the timeout is pending, cancel it.  If not using
Timeouts in <tt class="docutils literal"><span class="pre">with</span></tt> statements, always call cancel() in a
<tt class="docutils literal"><span class="pre">finally</span></tt> 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.</dd></dl>

<dl class="attribute">
<dt id="eventlet.timeout.eventlet.timeout.Timeout.Timeout.pending">
<tt class="descclassname">Timeout.</tt><tt class="descname">pending</tt><a class="headerlink" href="#eventlet.timeout.eventlet.timeout.Timeout.Timeout.pending" title="Permalink to this definition">¶</a></dt>
<dd>True if the timeout is scheduled to be raised.</dd></dl>

</dd></dl>

<dl class="function">
<dt id="eventlet.timeout.with_timeout">
<tt class="descclassname">eventlet.timeout.</tt><tt class="descname">with_timeout</tt><big>(</big><em>seconds</em>, <em>function</em>, <em>*args</em>, <em>**kwds</em><big>)</big><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>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><em>seconds</em> (int or float) &#8211; seconds before timeout occurs</li>
<li><em>func</em> &#8211; the callable to execute with a timeout; it must cooperatively yield, or else the timeout will not be able to trigger</li>
<li><em>*args</em> &#8211; positional arguments to pass to <em>func</em></li>
<li><em>**kwds</em> &#8211; keyword arguments to pass to <em>func</em></li>
<li><em>timeout_value</em> &#8211; value to return if timeout occurs (by default raises
<tt class="xref docutils literal"><span class="pre">Timeout</span></tt>)</li>
</ul>
</td>
</tr>
<tr class="field"><th class="field-name">Return type:</th><td class="field-body"><p class="first">Value returned by <em>func</em> if <em>func</em> returns before <em>seconds</em>, else
<em>timeout_value</em> if provided, else raises <tt class="xref docutils literal"><span class="pre">Timeout</span></tt>.</p>
</td>
</tr>
<tr class="field"><th class="field-name">Raises Timeout:</th><td class="field-body"><p class="first">if <em>func</em> times out and no <tt class="docutils literal"><span class="pre">timeout_value</span></tt> has
been provided.</p>
</td>
</tr>
<tr class="field"><th class="field-name">Exception:</th><td class="field-body"><p class="first last">Any exception raised by <em>func</em></p>
</td>
</tr>
</tbody>
</table>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><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="s">&#39;http://www.google.com/&#39;</span><span class="p">,</span> <span class="n">timeout_value</span><span class="o">=</span><span class="s">&quot;&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>Here <em>data</em> is either the result of the <tt class="docutils literal"><span class="pre">get()</span></tt> call, or the empty string
if it took too long to return.  Any exception raised by the <tt class="docutils literal"><span class="pre">get()</span></tt> call
is passed through to the caller.</p>
</dd></dl>

</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h4>Previous topic</h4>
            <p class="topless"><a href="semaphore.html"
                                  title="previous chapter"><tt class="docutils literal docutils literal docutils literal"><span class="pre">semaphore</span></tt> &#8211; Semaphore classes</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="websocket.html"
                                  title="next chapter"><tt class="docutils literal"><span class="pre">websocket</span></tt> &#8211; Websocket Server</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/modules/timeout.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" size="18" />
                <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="../modindex.html" title="Global 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><a href="../index.html">Eventlet v0.9.12 documentation</a> &raquo;</li>
          <li><a href="../modules.html" >Module Reference</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; Copyright 2005-2010, Eventlet Contributors.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.6.
    </div>
  </body>
</html>