Sophie

Sophie

distrib > Mandriva > 2010.2 > i586 > by-pkgid > b9ca70a7484981df9126f0365edf0863 > files > 241

python-pyzmq-2.2.0.1-1mdv2010.1.i586.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>eventloop.stack_context &mdash; PyZMQ v2.2.0.1 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:     '2.2.0.1',
        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="shortcut icon" href="../../_static/zeromq.ico"/>
    <link rel="top" title="PyZMQ v2.2.0.1 documentation" href="../../index.html" />
    <link rel="up" title="The PyZMQ API" href="../index.html" />
    <link rel="next" title="eventloop.zmqstream" href="zmq.eventloop.zmqstream.html" />
    <link rel="prev" title="eventloop.ioloop" href="zmq.eventloop.ioloop.html" /> 
  </head>
  <body>

<div style="background-color: white; text-align: left; padding: 10px 10px 15px 15px">
<a href="../../index.html"><img src="../../_static/logo.png" border="0" alt="PyZMQ Documentation"/></a>
</div>

    <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="zmq.eventloop.zmqstream.html" title="eventloop.zmqstream"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="zmq.eventloop.ioloop.html" title="eventloop.ioloop"
             accesskey="P">previous</a> |</li>
        <li><a href="../../index.html">home</a>|&nbsp;</li>
        <li><a href="../../search.html">search</a>|&nbsp;</li>
       <li><a href="../index.html">API</a> &raquo;</li>

          <li><a href="../index.html" accesskey="U">The PyZMQ API</a> &raquo;</li> 
      </ul>
    </div>

      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">eventloop.stack_context</a><ul>
<li><a class="reference internal" href="#module-zmq.eventloop.stack_context">Module: <tt class="docutils literal"><span class="pre">eventloop.stack_context</span></tt></a></li>
<li><a class="reference internal" href="#classes">Classes</a><ul>
<li><a class="reference internal" href="#exceptionstackcontext"><tt class="docutils literal"><span class="pre">ExceptionStackContext</span></tt></a></li>
<li><a class="reference internal" href="#nullcontext"><tt class="docutils literal"><span class="pre">NullContext</span></tt></a></li>
<li><a class="reference internal" href="#stackcontext"><tt class="docutils literal"><span class="pre">StackContext</span></tt></a></li>
</ul>
</li>
<li><a class="reference internal" href="#function">Function</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="zmq.eventloop.ioloop.html"
                        title="previous chapter">eventloop.ioloop</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="zmq.eventloop.zmqstream.html"
                        title="next chapter">eventloop.zmqstream</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../../_sources/api/generated/zmq.eventloop.stack_context.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="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="eventloop-stack-context">
<h1>eventloop.stack_context<a class="headerlink" href="#eventloop-stack-context" title="Permalink to this headline">¶</a></h1>
<div class="section" id="module-zmq.eventloop.stack_context">
<span id="module-eventloop-stack-context"></span><h2>Module: <tt class="xref py py-mod docutils literal"><span class="pre">eventloop.stack_context</span></tt><a class="headerlink" href="#module-zmq.eventloop.stack_context" title="Permalink to this headline">¶</a></h2>
<p>StackContext allows applications to maintain threadlocal-like state
that follows execution as it moves to other execution contexts.</p>
<p>The motivating examples are to eliminate the need for explicit
async_callback wrappers (as in tornado.web.RequestHandler), and to
allow some additional context to be kept for logging.</p>
<p>This is slightly magic, but it&#8217;s an extension of the idea that an exception
handler is a kind of stack-local state and when that stack is suspended
and resumed in a new context that state needs to be preserved.  StackContext
shifts the burden of restoring that state from each call site (e.g.
wrapping each AsyncHTTPClient callback in async_callback) to the mechanisms
that transfer control from one context to another (e.g. AsyncHTTPClient
itself, IOLoop, thread pools, etc).</p>
<p>Example usage:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@contextlib.contextmanager</span>
<span class="k">def</span> <span class="nf">die_on_error</span><span class="p">():</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">yield</span>
    <span class="k">except</span> <span class="ne">Exception</span><span class="p">:</span>
        <span class="n">logging</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">&quot;exception in asynchronous operation&quot;</span><span class="p">,</span><span class="n">exc_info</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>

<span class="k">with</span> <span class="n">StackContext</span><span class="p">(</span><span class="n">die_on_error</span><span class="p">):</span>
    <span class="c"># Any exception thrown here *or in callback and its desendents*</span>
    <span class="c"># will cause the process to exit instead of spinning endlessly</span>
    <span class="c"># in the ioloop.</span>
    <span class="n">http_client</span><span class="o">.</span><span class="n">fetch</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">callback</span><span class="p">)</span>
<span class="n">ioloop</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
</pre></div>
</div>
<p>Most applications shouln&#8217;t have to work with <cite>StackContext</cite> directly.
Here are a few rules of thumb for when it&#8217;s necessary:</p>
<ul class="simple">
<li>If you&#8217;re writing an asynchronous library that doesn&#8217;t rely on a
stack_context-aware library like <cite>tornado.ioloop</cite> or <cite>tornado.iostream</cite>
(for example, if you&#8217;re writing a thread pool), use
<cite>stack_context.wrap()</cite> before any asynchronous operations to capture the
stack context from where the operation was started.</li>
<li>If you&#8217;re writing an asynchronous library that has some shared
resources (such as a connection pool), create those shared resources
within a <tt class="docutils literal"><span class="pre">with</span> <span class="pre">stack_context.NullContext():</span></tt> block.  This will prevent
<tt class="docutils literal"><span class="pre">StackContexts</span></tt> from leaking from one request to another.</li>
<li>If you want to write something like an exception handler that will
persist across asynchronous calls, create a new <cite>StackContext</cite> (or
<cite>ExceptionStackContext</cite>), and make your asynchronous calls in a <tt class="docutils literal"><span class="pre">with</span></tt>
block that references your <cite>StackContext</cite>.</li>
</ul>
</div>
<div class="section" id="classes">
<h2>Classes<a class="headerlink" href="#classes" title="Permalink to this headline">¶</a></h2>
<div class="section" id="exceptionstackcontext">
<h3><a class="reference internal" href="#zmq.eventloop.stack_context.ExceptionStackContext" title="zmq.eventloop.stack_context.ExceptionStackContext"><tt class="xref py py-class docutils literal"><span class="pre">ExceptionStackContext</span></tt></a><a class="headerlink" href="#exceptionstackcontext" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="zmq.eventloop.stack_context.ExceptionStackContext">
<em class="property">class </em><tt class="descclassname">zmq.eventloop.stack_context.</tt><tt class="descname">ExceptionStackContext</tt><big>(</big><em>exception_handler</em><big>)</big><a class="headerlink" href="#zmq.eventloop.stack_context.ExceptionStackContext" title="Permalink to this definition">¶</a></dt>
<dd><p>Specialization of StackContext for exception handling.</p>
<p>The supplied exception_handler function will be called in the
event of an uncaught exception in this context.  The semantics are
similar to a try/finally clause, and intended use cases are to log
an error, close a socket, or similar cleanup actions.  The
exc_info triple (type, value, traceback) will be passed to the
exception_handler function.</p>
<p>If the exception handler returns true, the exception will be
consumed and will not be propagated to other exception handlers.</p>
</dd></dl>

</div>
<div class="section" id="nullcontext">
<h3><a class="reference internal" href="#zmq.eventloop.stack_context.NullContext" title="zmq.eventloop.stack_context.NullContext"><tt class="xref py py-class docutils literal"><span class="pre">NullContext</span></tt></a><a class="headerlink" href="#nullcontext" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="zmq.eventloop.stack_context.NullContext">
<em class="property">class </em><tt class="descclassname">zmq.eventloop.stack_context.</tt><tt class="descname">NullContext</tt><a class="headerlink" href="#zmq.eventloop.stack_context.NullContext" title="Permalink to this definition">¶</a></dt>
<dd><p>Resets the StackContext.</p>
<p>Useful when creating a shared resource on demand (e.g. an AsyncHTTPClient)
where the stack that caused the creating is not relevant to future
operations.</p>
</dd></dl>

</div>
<div class="section" id="stackcontext">
<h3><a class="reference internal" href="#zmq.eventloop.stack_context.StackContext" title="zmq.eventloop.stack_context.StackContext"><tt class="xref py py-class docutils literal"><span class="pre">StackContext</span></tt></a><a class="headerlink" href="#stackcontext" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="zmq.eventloop.stack_context.StackContext">
<em class="property">class </em><tt class="descclassname">zmq.eventloop.stack_context.</tt><tt class="descname">StackContext</tt><big>(</big><em>context_factory</em><big>)</big><a class="headerlink" href="#zmq.eventloop.stack_context.StackContext" title="Permalink to this definition">¶</a></dt>
<dd><p>Establishes the given context as a StackContext that will be transferred.</p>
<p>Note that the parameter is a callable that returns a context
manager, not the context itself.  That is, where for a
non-transferable context manager you would say:</p>
<div class="highlight-python"><pre>with my_context():</pre>
</div>
<p>StackContext takes the function itself rather than its result:</p>
<div class="highlight-python"><pre>with StackContext(my_context):</pre>
</div>
</dd></dl>

</div>
</div>
<div class="section" id="function">
<h2>Function<a class="headerlink" href="#function" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="zmq.eventloop.stack_context.wrap">
<tt class="descclassname">zmq.eventloop.stack_context.</tt><tt class="descname">wrap</tt><big>(</big><em>fn</em><big>)</big><a class="headerlink" href="#zmq.eventloop.stack_context.wrap" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a callable object that will restore the current StackContext
when executed.</p>
<p>Use this whenever saving a callback to be executed later in a
different execution context (either in a different thread or
asynchronously in the same thread).</p>
</dd></dl>

</div>
</div>


          </div>
        </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="zmq.eventloop.zmqstream.html" title="eventloop.zmqstream"
             >next</a> |</li>
        <li class="right" >
          <a href="zmq.eventloop.ioloop.html" title="eventloop.ioloop"
             >previous</a> |</li>
        <li><a href="../../index.html">home</a>|&nbsp;</li>
        <li><a href="../../search.html">search</a>|&nbsp;</li>
       <li><a href="../index.html">API</a> &raquo;</li>

          <li><a href="../index.html" >The PyZMQ API</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2010-2011, Brian E. Granger &amp; Min Ragan-Kelley.  
ØMQ logo © iMatix Corportation, used under the Creative Commons Attribution-Share Alike 3.0 License.  
Python logo ™ of the Python Software Foundation, used by Min RK with permission from the Foundation.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
    </div>
  </body>
</html>