deb-python-eventlet/doc/modules/event.html

266 lines
21 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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>event Cross-greenthread primitive &#8212; Eventlet 0.21.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.21.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="greenpool Green Thread Pools" href="greenpool.html" />
<link rel="prev" title="db_pool DBAPI 2 database connection pooling" href="db_pool.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="greenpool.html" title="greenpool Green Thread Pools"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="db_pool.html" title="db_pool DBAPI 2 database connection pooling"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="../index.html">Eventlet 0.21.0 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../modules.html" accesskey="U">Module Reference</a> &#187;</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="module-eventlet.event">
<span id="event-cross-greenthread-primitive"></span><h1><code class="xref py py-mod docutils literal"><span class="pre">event</span></code> &#8211; Cross-greenthread primitive<a class="headerlink" href="#module-eventlet.event" title="Permalink to this headline"></a></h1>
<dl class="class">
<dt id="eventlet.event.Event">
<em class="property">class </em><code class="descclassname">eventlet.event.</code><code class="descname">Event</code><a class="headerlink" href="#eventlet.event.Event" title="Permalink to this definition"></a></dt>
<dd><p>An abstraction where an arbitrary number of coroutines
can wait for one event from another.</p>
<p>Events are similar to a Queue that can only hold one item, but differ
in two important ways:</p>
<ol class="arabic simple">
<li>calling <a class="reference internal" href="#eventlet.event.Event.send" title="eventlet.event.Event.send"><code class="xref py py-meth docutils literal"><span class="pre">send()</span></code></a> never unschedules the current greenthread</li>
<li><a class="reference internal" href="#eventlet.event.Event.send" title="eventlet.event.Event.send"><code class="xref py py-meth docutils literal"><span class="pre">send()</span></code></a> can only be called once; create a new event to send again.</li>
</ol>
<p>They are good for communicating results between coroutines, and
are the basis for how
<a class="reference internal" href="greenthread.html#eventlet.greenthread.GreenThread.wait" title="eventlet.greenthread.GreenThread.wait"><code class="xref py py-meth docutils literal"><span class="pre">GreenThread.wait()</span></code></a>
is implemented.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">eventlet</span> <span class="k">import</span> <span class="n">event</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">eventlet</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">evt</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">Event</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">baz</span><span class="p">(</span><span class="n">b</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">evt</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">b</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">_</span> <span class="o">=</span> <span class="n">eventlet</span><span class="o">.</span><span class="n">spawn_n</span><span class="p">(</span><span class="n">baz</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">evt</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
<span class="go">4</span>
</pre></div>
</div>
<dl class="method">
<dt id="eventlet.event.Event.ready">
<code class="descname">ready</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#eventlet.event.Event.ready" title="Permalink to this definition"></a></dt>
<dd><p>Return true if the <a class="reference internal" href="#eventlet.event.Event.wait" title="eventlet.event.Event.wait"><code class="xref py py-meth docutils literal"><span class="pre">wait()</span></code></a> call will return immediately.
Used to avoid waiting for things that might take a while to time out.
For example, you can put a bunch of events into a list, and then visit
them all repeatedly, calling <a class="reference internal" href="#eventlet.event.Event.ready" title="eventlet.event.Event.ready"><code class="xref py py-meth docutils literal"><span class="pre">ready()</span></code></a> until one returns <code class="docutils literal"><span class="pre">True</span></code>,
and then you can <a class="reference internal" href="#eventlet.event.Event.wait" title="eventlet.event.Event.wait"><code class="xref py py-meth docutils literal"><span class="pre">wait()</span></code></a> on that one.</p>
</dd></dl>
<dl class="method">
<dt id="eventlet.event.Event.send">
<code class="descname">send</code><span class="sig-paren">(</span><em>result=None</em>, <em>exc=None</em><span class="sig-paren">)</span><a class="headerlink" href="#eventlet.event.Event.send" title="Permalink to this definition"></a></dt>
<dd><p>Makes arrangements for the waiters to be woken with the
result and then returns immediately to the parent.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">eventlet</span> <span class="k">import</span> <span class="n">event</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">eventlet</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">evt</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">Event</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">waiter</span><span class="p">():</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;about to wait&#39;</span><span class="p">)</span>
<span class="gp">... </span> <span class="n">result</span> <span class="o">=</span> <span class="n">evt</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;waited for </span><span class="si">{0}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">result</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">_</span> <span class="o">=</span> <span class="n">eventlet</span><span class="o">.</span><span class="n">spawn</span><span class="p">(</span><span class="n">waiter</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="mi">0</span><span class="p">)</span>
<span class="go">about to wait</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">evt</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="s1">&#39;a&#39;</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="mi">0</span><span class="p">)</span>
<span class="go">waited for a</span>
</pre></div>
</div>
<p>It is an error to call <a class="reference internal" href="#eventlet.event.Event.send" title="eventlet.event.Event.send"><code class="xref py py-meth docutils literal"><span class="pre">send()</span></code></a> multiple times on the same event.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">evt</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="s1">&#39;whoops&#39;</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">AssertionError</span>: <span class="n">Trying to re-send() an already-triggered event.</span>
</pre></div>
</div>
<p>Use <code class="xref py py-meth docutils literal"><span class="pre">reset()</span></code> between <a class="reference internal" href="#eventlet.event.Event.send" title="eventlet.event.Event.send"><code class="xref py py-meth docutils literal"><span class="pre">send()</span></code></a> s to reuse an event object.</p>
</dd></dl>
<dl class="method">
<dt id="eventlet.event.Event.send_exception">
<code class="descname">send_exception</code><span class="sig-paren">(</span><em>*args</em><span class="sig-paren">)</span><a class="headerlink" href="#eventlet.event.Event.send_exception" title="Permalink to this definition"></a></dt>
<dd><p>Same as <a class="reference internal" href="#eventlet.event.Event.send" title="eventlet.event.Event.send"><code class="xref py py-meth docutils literal"><span class="pre">send()</span></code></a>, but sends an exception to waiters.</p>
<p>The arguments to send_exception are the same as the arguments
to <code class="docutils literal"><span class="pre">raise</span></code>. If a single exception object is passed in, it
will be re-raised when <a class="reference internal" href="#eventlet.event.Event.wait" title="eventlet.event.Event.wait"><code class="xref py py-meth docutils literal"><span class="pre">wait()</span></code></a> is called, generating a
new stacktrace.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">eventlet</span> <span class="k">import</span> <span class="n">event</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">evt</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">Event</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">evt</span><span class="o">.</span><span class="n">send_exception</span><span class="p">(</span><span class="ne">RuntimeError</span><span class="p">())</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">evt</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
File <span class="nb">&quot;eventlet/event.py&quot;</span>, line <span class="m">120</span>, in <span class="n">wait</span>
<span class="n">current</span><span class="o">.</span><span class="n">throw</span><span class="p">(</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">_exc</span><span class="p">)</span>
<span class="gr">RuntimeError</span>
</pre></div>
</div>
<p>If it&#8217;s important to preserve the entire original stack trace,
you must pass in the entire <a class="reference external" href="https://docs.python.org/2/library/sys.html#sys.exc_info" title="(in Python v2.7)"><code class="xref py py-func docutils literal"><span class="pre">sys.exc_info()</span></code></a> tuple.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">sys</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">evt</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">Event</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">try</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">()</span>
<span class="gp">... </span><span class="k">except</span> <span class="ne">RuntimeError</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">evt</span><span class="o">.</span><span class="n">send_exception</span><span class="p">(</span><span class="o">*</span><span class="n">sys</span><span class="o">.</span><span class="n">exc_info</span><span class="p">())</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">evt</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
File <span class="nb">&quot;eventlet/event.py&quot;</span>, line <span class="m">120</span>, in <span class="n">wait</span>
<span class="n">current</span><span class="o">.</span><span class="n">throw</span><span class="p">(</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">_exc</span><span class="p">)</span>
File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">2</span>, in <span class="n">&lt;module&gt;</span>
<span class="gr">RuntimeError</span>
</pre></div>
</div>
<p>Note that doing so stores a traceback object directly on the
Event object, which may cause reference cycles. See the
<a class="reference external" href="https://docs.python.org/2/library/sys.html#sys.exc_info" title="(in Python v2.7)"><code class="xref py py-func docutils literal"><span class="pre">sys.exc_info()</span></code></a> documentation.</p>
</dd></dl>
<dl class="method">
<dt id="eventlet.event.Event.wait">
<code class="descname">wait</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#eventlet.event.Event.wait" title="Permalink to this definition"></a></dt>
<dd><p>Wait until another coroutine calls <a class="reference internal" href="#eventlet.event.Event.send" title="eventlet.event.Event.send"><code class="xref py py-meth docutils literal"><span class="pre">send()</span></code></a>.
Returns the value the other coroutine passed to
<a class="reference internal" href="#eventlet.event.Event.send" title="eventlet.event.Event.send"><code class="xref py py-meth docutils literal"><span class="pre">send()</span></code></a>.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">eventlet</span> <span class="k">import</span> <span class="n">event</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">eventlet</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">evt</span> <span class="o">=</span> <span class="n">event</span><span class="o">.</span><span class="n">Event</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">wait_on</span><span class="p">():</span>
<span class="gp">... </span> <span class="n">retval</span> <span class="o">=</span> <span class="n">evt</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;waited for </span><span class="si">{0}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">retval</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">_</span> <span class="o">=</span> <span class="n">eventlet</span><span class="o">.</span><span class="n">spawn</span><span class="p">(</span><span class="n">wait_on</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">evt</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="s1">&#39;result&#39;</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="mi">0</span><span class="p">)</span>
<span class="go">waited for result</span>
</pre></div>
</div>
<p>Returns immediately if the event has already
occurred.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">evt</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
<span class="go">&#39;result&#39;</span>
</pre></div>
</div>
</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="db_pool.html"
title="previous chapter"><code class="docutils literal"><span class="pre">db_pool</span></code> &#8211; DBAPI 2 database connection pooling</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="greenpool.html"
title="next chapter"><code class="docutils literal"><span class="pre">greenpool</span></code> &#8211; Green Thread Pools</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/modules/event.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="greenpool.html" title="greenpool Green Thread Pools"
>next</a> |</li>
<li class="right" >
<a href="db_pool.html" title="db_pool DBAPI 2 database connection pooling"
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="../index.html">Eventlet 0.21.0 documentation</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../modules.html" >Module Reference</a> &#187;</li>
</ul>
</div>
<div class="footer" role="contentinfo">
&#169; 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>