190 lines
11 KiB
HTML
190 lines
11 KiB
HTML
<!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 — Eventlet 0.20.0 documentation</title>
|
|
|
|
<link rel="stylesheet" href="_static/classic.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.20.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.20.0 documentation" href="index.html" />
|
|
<link rel="next" title="Zeromq" href="zeromq.html" />
|
|
<link rel="prev" title="Using SSL With Eventlet" href="ssl.html" />
|
|
</head>
|
|
<body role="document">
|
|
<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="zeromq.html" title="Zeromq"
|
|
accesskey="N">next</a> |</li>
|
|
<li class="right" >
|
|
<a href="ssl.html" title="Using SSL With Eventlet"
|
|
accesskey="P">previous</a> |</li>
|
|
<li class="nav-item nav-item-0"><a href="index.html">Eventlet 0.20.0 documentation</a> »</li>
|
|
</ul>
|
|
</div>
|
|
|
|
<div class="document">
|
|
<div class="documentwrapper">
|
|
<div class="bodywrapper">
|
|
<div class="body" role="main">
|
|
|
|
<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 ‘parent’ Python thread. It’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 “real” thread primitives and pipes. Fortunately, there’s little reason to use threads for concurrency when you’re already using coroutines.</p>
|
|
<p>The vast majority of the times you’ll want to use threads are to wrap some operation that is not “green”, such as a C library that uses its own OS calls to do socket operations. The <a class="reference internal" href="#module-eventlet.tpool" title="eventlet.tpool"><code class="xref py py-mod docutils literal"><span class="pre">tpool</span></code></a> module is provided to make these uses simpler.</p>
|
|
<p>The optional <a class="reference internal" href="hubs.html#understanding-hubs"><span>pyevent hub</span></a> is not compatible with threads.</p>
|
|
<div class="section" id="tpool-simple-thread-pool">
|
|
<h2>Tpool - Simple thread pool<a class="headerlink" href="#tpool-simple-thread-pool" title="Permalink to this headline">¶</a></h2>
|
|
<p>The simplest thing to do with <a class="reference internal" href="#module-eventlet.tpool" title="eventlet.tpool"><code class="xref py py-mod docutils literal"><span class="pre">tpool</span></code></a> is to <a class="reference internal" href="#eventlet.tpool.execute" title="eventlet.tpool.execute"><code class="xref py py-func docutils literal"><span class="pre">execute()</span></code></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">>>> </span><span class="kn">import</span> <span class="nn">thread</span>
|
|
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">eventlet</span> <span class="kn">import</span> <span class="n">tpool</span>
|
|
<span class="gp">>>> </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="p">(</span><span class="s2">"running in new thread:"</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">>>> </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 <code class="docutils literal"><span class="pre">EVENTLET_THREADPOOL_SIZE</span></code> to the desired pool size before importing tpool.</p>
|
|
<span class="target" id="module-eventlet.tpool"></span><dl class="function">
|
|
<dt id="eventlet.tpool.execute">
|
|
<code class="descclassname">eventlet.tpool.</code><code class="descname">execute</code><span class="sig-paren">(</span><em>meth</em>, <em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><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><code class="descclassname">eventlet.tpool.</code><code class="descname">Proxy</code><span class="sig-paren">(</span><em>obj</em>, <em>autowrap=()</em>, <em>autowrap_names=()</em><span class="sig-paren">)</span><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’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’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" 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="#">Threads</a><ul>
|
|
<li><a class="reference internal" href="#tpool-simple-thread-pool">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="zeromq.html"
|
|
title="next chapter">Zeromq</a></p>
|
|
<div role="note" aria-label="source link">
|
|
<h3>This Page</h3>
|
|
<ul class="this-page-menu">
|
|
<li><a href="_sources/threading.txt"
|
|
rel="nofollow">Show Source</a></li>
|
|
</ul>
|
|
</div>
|
|
<div id="searchbox" style="display: none" role="search">
|
|
<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" 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="zeromq.html" title="Zeromq"
|
|
>next</a> |</li>
|
|
<li class="right" >
|
|
<a href="ssl.html" title="Using SSL With Eventlet"
|
|
>previous</a> |</li>
|
|
<li class="nav-item nav-item-0"><a href="index.html">Eventlet 0.20.0 documentation</a> »</li>
|
|
</ul>
|
|
</div>
|
|
|
|
<div class="footer" role="contentinfo">
|
|
© Copyright 2005-2010, Eventlet Contributors.
|
|
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.5.
|
|
</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> |