249 lines
15 KiB
HTML
249 lines
15 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>greenpool – Green Thread Pools — 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,
|
||
SOURCELINK_SUFFIX: '.txt'
|
||
};
|
||
</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="index" title="Index" href="../genindex.html" />
|
||
<link rel="search" title="Search" href="../search.html" />
|
||
<link rel="next" title="greenthread – Green Thread Implementation" href="greenthread.html" />
|
||
<link rel="prev" title="event – Cross-greenthread primitive" href="event.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="greenthread.html" title="greenthread – Green Thread Implementation"
|
||
accesskey="N">next</a> |</li>
|
||
<li class="right" >
|
||
<a href="event.html" title="event – Cross-greenthread primitive"
|
||
accesskey="P">previous</a> |</li>
|
||
<li class="nav-item nav-item-0"><a href="../index.html">Eventlet 0.20.0 documentation</a> »</li>
|
||
<li class="nav-item nav-item-1"><a href="../modules.html" accesskey="U">Module Reference</a> »</li>
|
||
</ul>
|
||
</div>
|
||
|
||
<div class="document">
|
||
<div class="documentwrapper">
|
||
<div class="bodywrapper">
|
||
<div class="body" role="main">
|
||
|
||
<div class="section" id="module-eventlet.greenpool">
|
||
<span id="greenpool-green-thread-pools"></span><h1><code class="xref py py-mod docutils literal"><span class="pre">greenpool</span></code> – Green Thread Pools<a class="headerlink" href="#module-eventlet.greenpool" title="Permalink to this headline">¶</a></h1>
|
||
<dl class="class">
|
||
<dt id="eventlet.greenpool.GreenPool">
|
||
<em class="property">class </em><code class="descclassname">eventlet.greenpool.</code><code class="descname">GreenPool</code><span class="sig-paren">(</span><em>size=1000</em><span class="sig-paren">)</span><a class="headerlink" href="#eventlet.greenpool.GreenPool" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>The GreenPool class is a pool of green threads.</p>
|
||
<dl class="method">
|
||
<dt id="eventlet.greenpool.GreenPool.free">
|
||
<code class="descname">free</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#eventlet.greenpool.GreenPool.free" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>Returns the number of greenthreads available for use.</p>
|
||
<p>If zero or less, the next call to <a class="reference internal" href="#eventlet.greenpool.GreenPool.spawn" title="eventlet.greenpool.GreenPool.spawn"><code class="xref py py-meth docutils literal"><span class="pre">spawn()</span></code></a> or <a class="reference internal" href="#eventlet.greenpool.GreenPool.spawn_n" title="eventlet.greenpool.GreenPool.spawn_n"><code class="xref py py-meth docutils literal"><span class="pre">spawn_n()</span></code></a> will
|
||
block the calling greenthread until a slot becomes available.</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="eventlet.greenpool.GreenPool.imap">
|
||
<code class="descname">imap</code><span class="sig-paren">(</span><em>function</em>, <em>*iterables</em><span class="sig-paren">)</span><a class="headerlink" href="#eventlet.greenpool.GreenPool.imap" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>This is the same as <a class="reference external" href="https://docs.python.org/2/library/itertools.html#itertools.imap" title="(in Python v2.7)"><code class="xref py py-func docutils literal"><span class="pre">itertools.imap()</span></code></a>, and has the same
|
||
concurrency and memory behavior as <a class="reference internal" href="#eventlet.greenpool.GreenPool.starmap" title="eventlet.greenpool.GreenPool.starmap"><code class="xref py py-meth docutils literal"><span class="pre">starmap()</span></code></a>.</p>
|
||
<p>It’s quite convenient for, e.g., farming out jobs from a file:</p>
|
||
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">worker</span><span class="p">(</span><span class="n">line</span><span class="p">):</span>
|
||
<span class="k">return</span> <span class="n">do_something</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
|
||
<span class="n">pool</span> <span class="o">=</span> <span class="n">GreenPool</span><span class="p">()</span>
|
||
<span class="k">for</span> <span class="n">result</span> <span class="ow">in</span> <span class="n">pool</span><span class="o">.</span><span class="n">imap</span><span class="p">(</span><span class="n">worker</span><span class="p">,</span> <span class="nb">open</span><span class="p">(</span><span class="s2">"filename"</span><span class="p">,</span> <span class="s1">'r'</span><span class="p">)):</span>
|
||
<span class="nb">print</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="eventlet.greenpool.GreenPool.resize">
|
||
<code class="descname">resize</code><span class="sig-paren">(</span><em>new_size</em><span class="sig-paren">)</span><a class="headerlink" href="#eventlet.greenpool.GreenPool.resize" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>Change the max number of greenthreads doing work at any given time.</p>
|
||
<p>If resize is called when there are more than <em>new_size</em> greenthreads
|
||
already working on tasks, they will be allowed to complete but no new
|
||
tasks will be allowed to get launched until enough greenthreads finish
|
||
their tasks to drop the overall quantity below <em>new_size</em>. Until
|
||
then, the return value of free() will be negative.</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="eventlet.greenpool.GreenPool.running">
|
||
<code class="descname">running</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#eventlet.greenpool.GreenPool.running" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>Returns the number of greenthreads that are currently executing
|
||
functions in the GreenPool.</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="eventlet.greenpool.GreenPool.spawn">
|
||
<code class="descname">spawn</code><span class="sig-paren">(</span><em>function</em>, <em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#eventlet.greenpool.GreenPool.spawn" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>Run the <em>function</em> with its arguments in its own green thread.
|
||
Returns the <a class="reference internal" href="greenthread.html#eventlet.greenthread.GreenThread" title="eventlet.greenthread.GreenThread"><code class="xref py py-class docutils literal"><span class="pre">GreenThread</span></code></a>
|
||
object that is running the function, which can be used to retrieve the
|
||
results.</p>
|
||
<p>If the pool is currently at capacity, <code class="docutils literal"><span class="pre">spawn</span></code> will block until one of
|
||
the running greenthreads completes its task and frees up a slot.</p>
|
||
<p>This function is reentrant; <em>function</em> can call <code class="docutils literal"><span class="pre">spawn</span></code> on the same
|
||
pool without risk of deadlocking the whole thing.</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="eventlet.greenpool.GreenPool.spawn_n">
|
||
<code class="descname">spawn_n</code><span class="sig-paren">(</span><em>function</em>, <em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#eventlet.greenpool.GreenPool.spawn_n" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>Create a greenthread to run the <em>function</em>, the same as
|
||
<a class="reference internal" href="#eventlet.greenpool.GreenPool.spawn" title="eventlet.greenpool.GreenPool.spawn"><code class="xref py py-meth docutils literal"><span class="pre">spawn()</span></code></a>. The difference is that <a class="reference internal" href="#eventlet.greenpool.GreenPool.spawn_n" title="eventlet.greenpool.GreenPool.spawn_n"><code class="xref py py-meth docutils literal"><span class="pre">spawn_n()</span></code></a> returns
|
||
None; the results of <em>function</em> are not retrievable.</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="eventlet.greenpool.GreenPool.starmap">
|
||
<code class="descname">starmap</code><span class="sig-paren">(</span><em>function</em>, <em>iterable</em><span class="sig-paren">)</span><a class="headerlink" href="#eventlet.greenpool.GreenPool.starmap" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>This is the same as <a class="reference external" href="https://docs.python.org/2/library/itertools.html#itertools.starmap" title="(in Python v2.7)"><code class="xref py py-func docutils literal"><span class="pre">itertools.starmap()</span></code></a>, except that <em>func</em> is
|
||
executed in a separate green thread for each item, with the concurrency
|
||
limited by the pool’s size. In operation, starmap consumes a constant
|
||
amount of memory, proportional to the size of the pool, and is thus
|
||
suited for iterating over extremely long input lists.</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="eventlet.greenpool.GreenPool.waitall">
|
||
<code class="descname">waitall</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#eventlet.greenpool.GreenPool.waitall" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>Waits until all greenthreads in the pool are finished working.</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="eventlet.greenpool.GreenPool.waiting">
|
||
<code class="descname">waiting</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#eventlet.greenpool.GreenPool.waiting" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>Return the number of greenthreads waiting to spawn.</p>
|
||
</dd></dl>
|
||
|
||
</dd></dl>
|
||
|
||
<dl class="class">
|
||
<dt id="eventlet.greenpool.GreenPile">
|
||
<em class="property">class </em><code class="descclassname">eventlet.greenpool.</code><code class="descname">GreenPile</code><span class="sig-paren">(</span><em>size_or_pool=1000</em><span class="sig-paren">)</span><a class="headerlink" href="#eventlet.greenpool.GreenPile" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>GreenPile is an abstraction representing a bunch of I/O-related tasks.</p>
|
||
<p>Construct a GreenPile with an existing GreenPool object. The GreenPile will
|
||
then use that pool’s concurrency as it processes its jobs. There can be
|
||
many GreenPiles associated with a single GreenPool.</p>
|
||
<p>A GreenPile can also be constructed standalone, not associated with any
|
||
GreenPool. To do this, construct it with an integer size parameter instead
|
||
of a GreenPool.</p>
|
||
<p>It is not advisable to iterate over a GreenPile in a different greenthread
|
||
than the one which is calling spawn. The iterator will exit early in that
|
||
situation.</p>
|
||
<dl class="method">
|
||
<dt id="eventlet.greenpool.GreenPile.next">
|
||
<code class="descname">next</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#eventlet.greenpool.GreenPile.next" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>Wait for the next result, suspending the current greenthread until it
|
||
is available. Raises StopIteration when there are no more results.</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="eventlet.greenpool.GreenPile.spawn">
|
||
<code class="descname">spawn</code><span class="sig-paren">(</span><em>func</em>, <em>*args</em>, <em>**kw</em><span class="sig-paren">)</span><a class="headerlink" href="#eventlet.greenpool.GreenPile.spawn" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>Runs <em>func</em> in its own green thread, with the result available by
|
||
iterating over the GreenPile object.</p>
|
||
</dd></dl>
|
||
|
||
</dd></dl>
|
||
|
||
</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="event.html"
|
||
title="previous chapter"><code class="docutils literal"><span class="pre">event</span></code> – Cross-greenthread primitive</a></p>
|
||
<h4>Next topic</h4>
|
||
<p class="topless"><a href="greenthread.html"
|
||
title="next chapter"><code class="docutils literal"><span class="pre">greenthread</span></code> – Green Thread Implementation</a></p>
|
||
<div role="note" aria-label="source link">
|
||
<h3>This Page</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../_sources/modules/greenpool.rst.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">
|
||
<div><input type="text" name="q" /></div>
|
||
<div><input type="submit" value="Go" /></div>
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</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="greenthread.html" title="greenthread – Green Thread Implementation"
|
||
>next</a> |</li>
|
||
<li class="right" >
|
||
<a href="event.html" title="event – Cross-greenthread primitive"
|
||
>previous</a> |</li>
|
||
<li class="nav-item nav-item-0"><a href="../index.html">Eventlet 0.20.0 documentation</a> »</li>
|
||
<li class="nav-item nav-item-1"><a href="../modules.html" >Module Reference</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.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> |