<!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>greenthread – Green Thread Implementation — Eventlet 0.13.0 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.13.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.13.0 documentation" href="../index.html" /> <link rel="up" title="Module Reference" href="../modules.html" /> <link rel="next" title="pools - Generic pools of resources" href="pools.html" /> <link rel="prev" title="greenpool – Green Thread Pools" href="greenpool.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="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="pools.html" title="pools - Generic pools of resources" accesskey="N">next</a> |</li> <li class="right" > <a href="greenpool.html" title="greenpool – Green Thread Pools" accesskey="P">previous</a> |</li> <li><a href="../index.html">Eventlet 0.13.0 documentation</a> »</li> <li><a href="../modules.html" accesskey="U">Module Reference</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="module-eventlet.greenthread"> <span id="greenthread-green-thread-implementation"></span><h1><tt class="xref py py-mod docutils literal"><span class="pre">greenthread</span></tt> – Green Thread Implementation<a class="headerlink" href="#module-eventlet.greenthread" title="Permalink to this headline">¶</a></h1> <dl class="function"> <dt id="eventlet.greenthread.sleep"> <tt class="descclassname">eventlet.greenthread.</tt><tt class="descname">sleep</tt><big>(</big><em>seconds=0</em><big>)</big><a class="headerlink" href="#eventlet.greenthread.sleep" title="Permalink to this definition">¶</a></dt> <dd><p>Yield control to another eligible coroutine until at least <em>seconds</em> have elapsed.</p> <p><em>seconds</em> may be specified as an integer, or a float if fractional seconds are desired. Calling <tt class="xref py py-func docutils literal"><span class="pre">sleep()</span></tt> with <em>seconds</em> of 0 is the canonical way of expressing a cooperative yield. For example, if one is looping over a large list performing an expensive calculation without calling any socket methods, it’s a good idea to call <tt class="docutils literal"><span class="pre">sleep(0)</span></tt> occasionally; otherwise nothing else will run.</p> </dd></dl> <dl class="function"> <dt id="eventlet.greenthread.spawn"> <tt class="descclassname">eventlet.greenthread.</tt><tt class="descname">spawn</tt><big>(</big><em>func</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#eventlet.greenthread.spawn" title="Permalink to this definition">¶</a></dt> <dd><p>Create a greenthread to run <tt class="docutils literal"><span class="pre">func(*args,</span> <span class="pre">**kwargs)</span></tt>. Returns a <a class="reference internal" href="#eventlet.greenthread.GreenThread" title="eventlet.greenthread.GreenThread"><tt class="xref py py-class docutils literal"><span class="pre">GreenThread</span></tt></a> object which you can use to get the results of the call.</p> <p>Execution control returns immediately to the caller; the created greenthread is merely scheduled to be run at the next available opportunity. Use <a class="reference internal" href="#eventlet.greenthread.spawn_after" title="eventlet.greenthread.spawn_after"><tt class="xref py py-func docutils literal"><span class="pre">spawn_after()</span></tt></a> to arrange for greenthreads to be spawned after a finite delay.</p> </dd></dl> <dl class="function"> <dt id="eventlet.greenthread.spawn_n"> <tt class="descclassname">eventlet.greenthread.</tt><tt class="descname">spawn_n</tt><big>(</big><em>func</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#eventlet.greenthread.spawn_n" title="Permalink to this definition">¶</a></dt> <dd><p>Same as <a class="reference internal" href="#eventlet.greenthread.spawn" title="eventlet.greenthread.spawn"><tt class="xref py py-func docutils literal"><span class="pre">spawn()</span></tt></a>, but returns a <tt class="docutils literal"><span class="pre">greenlet</span></tt> object from which it is not possible to retrieve either a return value or whether it raised any exceptions. This is faster than <a class="reference internal" href="#eventlet.greenthread.spawn" title="eventlet.greenthread.spawn"><tt class="xref py py-func docutils literal"><span class="pre">spawn()</span></tt></a>; it is fastest if there are no keyword arguments.</p> <p>If an exception is raised in the function, spawn_n prints a stack trace; the print can be disabled by calling <a class="reference internal" href="debug.html#eventlet.debug.hub_exceptions" title="eventlet.debug.hub_exceptions"><tt class="xref py py-func docutils literal"><span class="pre">eventlet.debug.hub_exceptions()</span></tt></a> with False.</p> </dd></dl> <dl class="function"> <dt id="eventlet.greenthread.spawn_after"> <tt class="descclassname">eventlet.greenthread.</tt><tt class="descname">spawn_after</tt><big>(</big><em>seconds</em>, <em>func</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#eventlet.greenthread.spawn_after" title="Permalink to this definition">¶</a></dt> <dd><p>Spawns <em>func</em> after <em>seconds</em> have elapsed. It runs as scheduled even if the current greenthread has completed.</p> <p><em>seconds</em> may be specified as an integer, or a float if fractional seconds are desired. The <em>func</em> will be called with the given <em>args</em> and keyword arguments <em>kwargs</em>, and will be executed within its own greenthread.</p> <p>The return value of <a class="reference internal" href="#eventlet.greenthread.spawn_after" title="eventlet.greenthread.spawn_after"><tt class="xref py py-func docutils literal"><span class="pre">spawn_after()</span></tt></a> is a <a class="reference internal" href="#eventlet.greenthread.GreenThread" title="eventlet.greenthread.GreenThread"><tt class="xref py py-class docutils literal"><span class="pre">GreenThread</span></tt></a> object, which can be used to retrieve the results of the call.</p> <p>To cancel the spawn and prevent <em>func</em> from being called, call <a class="reference internal" href="#eventlet.greenthread.GreenThread.cancel" title="eventlet.greenthread.GreenThread.cancel"><tt class="xref py py-meth docutils literal"><span class="pre">GreenThread.cancel()</span></tt></a> on the return value of <a class="reference internal" href="#eventlet.greenthread.spawn_after" title="eventlet.greenthread.spawn_after"><tt class="xref py py-func docutils literal"><span class="pre">spawn_after()</span></tt></a>. This will not abort the function if it’s already started running, which is generally the desired behavior. If terminating <em>func</em> regardless of whether it’s started or not is the desired behavior, call <a class="reference internal" href="#eventlet.greenthread.GreenThread.kill" title="eventlet.greenthread.GreenThread.kill"><tt class="xref py py-meth docutils literal"><span class="pre">GreenThread.kill()</span></tt></a>.</p> </dd></dl> <dl class="function"> <dt id="eventlet.greenthread.spawn_after_local"> <tt class="descclassname">eventlet.greenthread.</tt><tt class="descname">spawn_after_local</tt><big>(</big><em>seconds</em>, <em>func</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#eventlet.greenthread.spawn_after_local" title="Permalink to this definition">¶</a></dt> <dd><p>Spawns <em>func</em> after <em>seconds</em> have elapsed. The function will NOT be called if the current greenthread has exited.</p> <p><em>seconds</em> may be specified as an integer, or a float if fractional seconds are desired. The <em>func</em> will be called with the given <em>args</em> and keyword arguments <em>kwargs</em>, and will be executed within its own greenthread.</p> <p>The return value of <a class="reference internal" href="#eventlet.greenthread.spawn_after" title="eventlet.greenthread.spawn_after"><tt class="xref py py-func docutils literal"><span class="pre">spawn_after()</span></tt></a> is a <a class="reference internal" href="#eventlet.greenthread.GreenThread" title="eventlet.greenthread.GreenThread"><tt class="xref py py-class docutils literal"><span class="pre">GreenThread</span></tt></a> object, which can be used to retrieve the results of the call.</p> <p>To cancel the spawn and prevent <em>func</em> from being called, call <a class="reference internal" href="#eventlet.greenthread.GreenThread.cancel" title="eventlet.greenthread.GreenThread.cancel"><tt class="xref py py-meth docutils literal"><span class="pre">GreenThread.cancel()</span></tt></a> on the return value. This will not abort the function if it’s already started running. If terminating <em>func</em> regardless of whether it’s started or not is the desired behavior, call <a class="reference internal" href="#eventlet.greenthread.GreenThread.kill" title="eventlet.greenthread.GreenThread.kill"><tt class="xref py py-meth docutils literal"><span class="pre">GreenThread.kill()</span></tt></a>.</p> </dd></dl> <dl class="class"> <dt id="eventlet.greenthread.GreenThread"> <em class="property">class </em><tt class="descclassname">eventlet.greenthread.</tt><tt class="descname">GreenThread</tt><big>(</big><em>parent</em><big>)</big><a class="headerlink" href="#eventlet.greenthread.GreenThread" title="Permalink to this definition">¶</a></dt> <dd><p>The GreenThread class is a type of Greenlet which has the additional property of being able to retrieve the return value of the main function. Do not construct GreenThread objects directly; call <a class="reference internal" href="#eventlet.greenthread.spawn" title="eventlet.greenthread.spawn"><tt class="xref py py-func docutils literal"><span class="pre">spawn()</span></tt></a> to get one.</p> <dl class="method"> <dt id="eventlet.greenthread.GreenThread.cancel"> <tt class="descname">cancel</tt><big>(</big><em>*throw_args</em><big>)</big><a class="headerlink" href="#eventlet.greenthread.GreenThread.cancel" title="Permalink to this definition">¶</a></dt> <dd><p>Kills the greenthread using <a class="reference internal" href="#eventlet.greenthread.GreenThread.kill" title="eventlet.greenthread.GreenThread.kill"><tt class="xref py py-func docutils literal"><span class="pre">kill()</span></tt></a>, but only if it hasn’t already started running. After being canceled, all calls to <a class="reference internal" href="#eventlet.greenthread.GreenThread.wait" title="eventlet.greenthread.GreenThread.wait"><tt class="xref py py-meth docutils literal"><span class="pre">wait()</span></tt></a> will raise <em>throw_args</em> (which default to <tt class="xref py py-class docutils literal"><span class="pre">greenlet.GreenletExit</span></tt>).</p> </dd></dl> <dl class="method"> <dt id="eventlet.greenthread.GreenThread.kill"> <tt class="descname">kill</tt><big>(</big><em>*throw_args</em><big>)</big><a class="headerlink" href="#eventlet.greenthread.GreenThread.kill" title="Permalink to this definition">¶</a></dt> <dd><p>Kills the greenthread using <a class="reference internal" href="#eventlet.greenthread.GreenThread.kill" title="eventlet.greenthread.GreenThread.kill"><tt class="xref py py-func docutils literal"><span class="pre">kill()</span></tt></a>. After being killed all calls to <a class="reference internal" href="#eventlet.greenthread.GreenThread.wait" title="eventlet.greenthread.GreenThread.wait"><tt class="xref py py-meth docutils literal"><span class="pre">wait()</span></tt></a> will raise <em>throw_args</em> (which default to <tt class="xref py py-class docutils literal"><span class="pre">greenlet.GreenletExit</span></tt>).</p> </dd></dl> <dl class="method"> <dt id="eventlet.greenthread.GreenThread.link"> <tt class="descname">link</tt><big>(</big><em>func</em>, <em>*curried_args</em>, <em>**curried_kwargs</em><big>)</big><a class="headerlink" href="#eventlet.greenthread.GreenThread.link" title="Permalink to this definition">¶</a></dt> <dd><p>Set up a function to be called with the results of the GreenThread.</p> <p>The function must have the following signature:</p> <div class="highlight-python"><pre>def func(gt, [curried args/kwargs]):</pre> </div> <p>When the GreenThread finishes its run, it calls <em>func</em> with itself and with the <a class="reference external" href="http://en.wikipedia.org/wiki/Currying">curried arguments</a> supplied at link-time. If the function wants to retrieve the result of the GreenThread, it should call wait() on its first argument.</p> <p>Note that <em>func</em> is called within execution context of the GreenThread, so it is possible to interfere with other linked functions by doing things like switching explicitly to another greenthread.</p> </dd></dl> <dl class="method"> <dt id="eventlet.greenthread.GreenThread.wait"> <tt class="descname">wait</tt><big>(</big><big>)</big><a class="headerlink" href="#eventlet.greenthread.GreenThread.wait" title="Permalink to this definition">¶</a></dt> <dd><p>Returns the result of the main function of this GreenThread. If the result is a normal return value, <a class="reference internal" href="#eventlet.greenthread.GreenThread.wait" title="eventlet.greenthread.GreenThread.wait"><tt class="xref py py-meth docutils literal"><span class="pre">wait()</span></tt></a> returns it. If it raised an exception, <a class="reference internal" href="#eventlet.greenthread.GreenThread.wait" title="eventlet.greenthread.GreenThread.wait"><tt class="xref py py-meth docutils literal"><span class="pre">wait()</span></tt></a> will raise the same exception (though the stack trace will unavoidably contain some frames from within the greenthread module).</p> </dd></dl> </dd></dl> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h4>Previous topic</h4> <p class="topless"><a href="greenpool.html" title="previous chapter"><tt class="docutils literal"><span class="pre">greenpool</span></tt> – Green Thread Pools</a></p> <h4>Next topic</h4> <p class="topless"><a href="pools.html" title="next chapter"><tt class="docutils literal"><span class="pre">pools</span></tt> - Generic pools of resources</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/modules/greenthread.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" /> <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="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="pools.html" title="pools - Generic pools of resources" >next</a> |</li> <li class="right" > <a href="greenpool.html" title="greenpool – Green Thread Pools" >previous</a> |</li> <li><a href="../index.html">Eventlet 0.13.0 documentation</a> »</li> <li><a href="../modules.html" >Module Reference</a> »</li> </ul> </div> <div class="footer"> © Copyright 2005-2010, Eventlet Contributors. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>