Sophie

Sophie

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

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>Threads &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="next" title="Understanding Eventlet Hubs" href="hubs.html" />
    <link rel="prev" title="Using SSL With Eventlet" href="ssl.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="hubs.html" title="Understanding Eventlet Hubs"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="ssl.html" title="Using SSL With Eventlet"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Eventlet v0.9.12 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="threads">
<h1>Threads<a class="headerlink" href="#threads" title="Permalink to this headline">¶</a></h1>
<p>Eventlet is thread-safe and can be used in conjunction with normal Python threads.  The way this works is that coroutines are confined to their &#8216;parent&#8217; Python thread.  It&#8217;s like each thread contains its own little world of coroutines that can switch between themselves but not between coroutines in other threads.</p>
<img alt="_images/threading_illustration.png" src="_images/threading_illustration.png" />
<p>You can only communicate cross-thread using the &#8220;real&#8221; thread primitives and pipes.  Fortunately, there&#8217;s little reason to use threads for concurrency when you&#8217;re already using coroutines.</p>
<p>The vast majority of the times you&#8217;ll want to use threads are to wrap some operation that is not &#8220;green&#8221;, such as a C library that uses its own OS calls to do socket operations.  The <tt class="xref docutils literal"><span class="pre">tpool</span></tt> module is provided to make these uses simpler.</p>
<p>The optional <a class="reference external" href="hubs.html#understanding-hubs"><em>pyevent hub</em></a> is not compatible with threads.</p>
<div class="section" id="module-eventlet.tpool">
<h2>Tpool - Simple thread pool<a class="headerlink" href="#module-eventlet.tpool" title="Permalink to this headline">¶</a></h2>
<p>The simplest thing to do with <tt class="xref docutils literal"><span class="pre">tpool</span></tt> is to <a title="eventlet.tpool.execute" class="reference internal" href="#eventlet.tpool.execute"><tt class="xref docutils literal"><span class="pre">execute()</span></tt></a> a function with it.  The function will be run in a random thread in the pool, while the calling coroutine blocks on its completion:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">thread</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">eventlet</span> <span class="kn">import</span> <span class="n">tpool</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">my_func</span><span class="p">(</span><span class="n">starting_ident</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">print</span> <span class="s">&quot;running in new thread:&quot;</span><span class="p">,</span> <span class="n">starting_ident</span> <span class="o">!=</span> <span class="n">thread</span><span class="o">.</span><span class="n">get_ident</span><span class="p">()</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tpool</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">my_func</span><span class="p">,</span> <span class="n">thread</span><span class="o">.</span><span class="n">get_ident</span><span class="p">())</span>
<span class="go">running in new thread: True</span>
</pre></div>
</div>
<p>By default there are 20 threads in the pool, but you can configure this by setting the environment variable <tt class="docutils literal"><span class="pre">EVENTLET_THREADPOOL_SIZE</span></tt> to the desired pool size before importing tpool.</p>
<dl class="function">
<dt id="eventlet.tpool.execute">
<tt class="descclassname">eventlet.tpool.</tt><tt class="descname">execute</tt><big>(</big><em>meth</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#eventlet.tpool.execute" title="Permalink to this definition">¶</a></dt>
<dd><p>Execute <em>meth</em> in a Python thread, blocking the current coroutine/
greenthread until the method completes.</p>
<p>The primary use case for this is to wrap an object or module that is not
amenable to monkeypatching or any of the other tricks that Eventlet uses
to achieve cooperative yielding.  With tpool, you can force such objects to
cooperate with green threads by sticking them in native threads, at the cost
of some overhead.</p>
</dd></dl>

<dl class="class">
<dt id="eventlet.tpool.Proxy">
<em class="property">class </em><tt class="descclassname">eventlet.tpool.</tt><tt class="descname">Proxy</tt><big>(</big><em>obj</em>, <em>autowrap=()</em>, <em>autowrap_names=()</em><big>)</big><a class="headerlink" href="#eventlet.tpool.Proxy" title="Permalink to this definition">¶</a></dt>
<dd><p>a simple proxy-wrapper of any object that comes with a
methods-only interface, in order to forward every method
invocation onto a thread in the native-thread pool.  A key
restriction is that the object&#8217;s methods should not switch
greenlets or use Eventlet primitives, since they are in a
different thread from the main hub, and therefore might behave
unexpectedly.  This is for running native-threaded code
only.</p>
<p>It&#8217;s common to want to have some of the attributes or return
values also wrapped in Proxy objects (for example, database
connection objects produce cursor objects which also should be
wrapped in Proxy objects to remain nonblocking).  <em>autowrap</em>, if
supplied, is a collection of types; if an attribute or return
value matches one of those types (via isinstance), it will be
wrapped in a Proxy.  <em>autowrap_names</em> is a collection
of strings, which represent the names of attributes that should be
wrapped in Proxy objects when accessed.</p>
</dd></dl>

</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 external" href="#">Threads</a><ul>
<li><a class="reference external" href="#module-eventlet.tpool">Tpool - Simple thread pool</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="ssl.html"
                                  title="previous chapter">Using SSL With Eventlet</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="hubs.html"
                                  title="next chapter">Understanding Eventlet Hubs</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="_sources/threading.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="hubs.html" title="Understanding Eventlet Hubs"
             >next</a> |</li>
        <li class="right" >
          <a href="ssl.html" title="Using SSL With Eventlet"
             >previous</a> |</li>
        <li><a href="index.html">Eventlet v0.9.12 documentation</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>