Sophie

Sophie

distrib > Mandriva > 2010.2 > i586 > media > contrib-backports > by-pkgid > b9ca70a7484981df9126f0365edf0863 > files > 252

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>Devices in PyZMQ &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="next" title="Eventloops and PyZMQ" href="eventloop.html" />
    <link rel="prev" title="Serializing messages with PyZMQ" href="serialization.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="eventloop.html" title="Eventloops and PyZMQ"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="serialization.html" title="Serializing messages with PyZMQ"
             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="api/index.html">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="#">Devices in PyZMQ</a><ul>
<li><a class="reference internal" href="#backgrounddevices">BackgroundDevices</a></li>
<li><a class="reference internal" href="#monitoredqueue">MonitoredQueue</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="serialization.html"
                        title="previous chapter">Serializing messages with PyZMQ</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="eventloop.html"
                        title="next chapter">Eventloops and PyZMQ</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="_sources/devices.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="devices-in-pyzmq">
<span id="devices"></span><h1>Devices in PyZMQ<a class="headerlink" href="#devices-in-pyzmq" title="Permalink to this headline">¶</a></h1>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last">ØMQ Guide <a class="reference external" href="http://zguide.zeromq.org/chapter:all#toc32">Device coverage</a>.</p>
</div>
<p>ØMQ has a notion of Devices - simple programs that manage a send-recv pattern for
connecting two or more sockets. Being full programs, devices include a <tt class="docutils literal"><span class="pre">while(True)</span></tt>
loop and thus block execution permanently once invoked. We have provided in the
<tt class="xref py py-mod docutils literal"><span class="pre">devices</span></tt> subpackage some facilities for running these devices in the background, as
well as a custom three-socket <a class="reference internal" href="#monitoredqueue">MonitoredQueue</a> device.</p>
<div class="section" id="backgrounddevices">
<h2>BackgroundDevices<a class="headerlink" href="#backgrounddevices" title="Permalink to this headline">¶</a></h2>
<p>It seems fairly rare that in a Python program one would actually want to create a zmq
device via <tt class="xref py py-func docutils literal"><span class="pre">device()</span></tt> in the main thread, since such a call would block execution
forever. The most likely model for launching devices is in background threads or
processes. We have provided classes for launching devices in a background thread with
<a class="reference internal" href="api/generated/zmq.devices.basedevice.html#zmq.devices.basedevice.ThreadDevice" title="zmq.devices.basedevice.ThreadDevice"><tt class="xref py py-class docutils literal"><span class="pre">ThreadDevice</span></tt></a> and via multiprocessing with <a class="reference internal" href="api/generated/zmq.devices.basedevice.html#zmq.devices.basedevice.ProcessDevice" title="zmq.devices.basedevice.ProcessDevice"><tt class="xref py py-class docutils literal"><span class="pre">ProcessDevice</span></tt></a>. For
threadsafety and running across processes, these methods do not take Socket objects as
arguments, but rather socket types, and then the socket creation and configuration happens
via the BackgroundDevice&#8217;s <tt class="xref py py-meth docutils literal"><span class="pre">foo_in()</span></tt> proxy methods. For each configuration method
(bind/connect/setsockopt), there are proxy methods for calling those methods on the Socket
objects created in the background thread or process, prefixed with &#8216;in_&#8217; or &#8216;out_&#8217;,
corresponding to the <cite>in_socket</cite> and <cite>out_socket</cite>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">zmq.devices</span> <span class="kn">import</span> <span class="n">ProcessDevice</span>

<span class="n">pd</span> <span class="o">=</span> <span class="n">ProcessDevice</span><span class="p">(</span><span class="n">zmq</span><span class="o">.</span><span class="n">QUEUE</span><span class="p">,</span> <span class="n">zmq</span><span class="o">.</span><span class="n">REP</span><span class="p">,</span> <span class="n">zmq</span><span class="o">.</span><span class="n">REQ</span><span class="p">)</span>
<span class="n">pd</span><span class="o">.</span><span class="n">bind_in</span><span class="p">(</span><span class="s">&#39;tcp://*:12345&#39;</span><span class="p">)</span>
<span class="n">pd</span><span class="o">.</span><span class="n">connect_out</span><span class="p">(</span><span class="s">&#39;tcp://127.0.0.1:12543&#39;</span><span class="p">)</span>
<span class="n">pd</span><span class="o">.</span><span class="n">setsockopt_in</span><span class="p">(</span><span class="n">zmq</span><span class="o">.</span><span class="n">IDENTITY</span><span class="p">,</span> <span class="s">&#39;REP&#39;</span><span class="p">)</span>
<span class="n">pd</span><span class="o">.</span><span class="n">setsockopt_out</span><span class="p">(</span><span class="n">zmq</span><span class="o">.</span><span class="n">IDENTITY</span><span class="p">,</span> <span class="s">&#39;REQ&#39;</span><span class="p">)</span>
<span class="n">pd</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
<span class="c"># it will now be running in a background process</span>
</pre></div>
</div>
</div>
<div class="section" id="monitoredqueue">
<h2>MonitoredQueue<a class="headerlink" href="#monitoredqueue" title="Permalink to this headline">¶</a></h2>
<p>One of ØMQ&#8217;s builtin devices is the <tt class="docutils literal"><span class="pre">QUEUE</span></tt>. This is a symmetric two-socket device that
fully supports passing messages in either direction via any pattern. We saw a logical
extension of the <tt class="docutils literal"><span class="pre">QUEUE</span></tt> as one that behaves in the same way with respect to the in/out
sockets, but also sends every message in either direction <em>also</em> on a third <cite>monitor</cite>
socket. For performance reasons, this <tt class="xref py py-func docutils literal"><span class="pre">monitored_queue()</span></tt> function is written in
Cython, so the loop does not involve Python, and should have the same performance as the
basic <tt class="docutils literal"><span class="pre">QUEUE</span></tt> device.</p>
<p>One shortcoming of the <tt class="docutils literal"><span class="pre">QUEUE</span></tt> device is that it does not support having <tt class="docutils literal"><span class="pre">ROUTER</span></tt>
sockets as both input and output. This is because <tt class="docutils literal"><span class="pre">ROUTER</span></tt> sockets, when they receive a
message, prepend the <tt class="docutils literal"><span class="pre">IDENTITY</span></tt> of the socket that sent the message (for use in routing
the reply). The result is that the output socket will always try to route the incoming
message back to the original sender, which is presumably not the intended pattern. In
order for the queue to support a ROUTER-ROUTER connection, it must swap the first two parts
of the message in order to get the right message out the other side.</p>
<p>To invoke a monitored queue is similar to invoking a regular ØMQ device:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">zmq.devices</span> <span class="kn">import</span> <span class="n">monitored_queue</span>
<span class="n">ins</span> <span class="o">=</span> <span class="n">ctx</span><span class="o">.</span><span class="n">socket</span><span class="p">(</span><span class="n">zmq</span><span class="o">.</span><span class="n">ROUTER</span><span class="p">)</span>
<span class="n">outs</span> <span class="o">=</span> <span class="n">ctx</span><span class="o">.</span><span class="n">socket</span><span class="p">(</span><span class="n">zmq</span><span class="o">.</span><span class="n">DEALER</span><span class="p">)</span>
<span class="n">mons</span> <span class="o">=</span> <span class="n">ctx</span><span class="o">.</span><span class="n">socket</span><span class="p">(</span><span class="n">zmq</span><span class="o">.</span><span class="n">PUB</span><span class="p">)</span>
<span class="n">configure_sockets</span><span class="p">(</span><span class="n">ins</span><span class="p">,</span><span class="n">outs</span><span class="p">,</span><span class="n">mons</span><span class="p">)</span>
<span class="n">monitored_queue</span><span class="p">(</span><span class="n">ins</span><span class="p">,</span> <span class="n">outs</span><span class="p">,</span> <span class="n">mons</span><span class="p">,</span> <span class="n">in_prefix</span><span class="o">=</span><span class="s">&#39;in&#39;</span><span class="p">,</span> <span class="n">out_prefix</span><span class="o">=</span><span class="s">&#39;out&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>The <cite>in_prefix</cite> and <cite>out_prefix</cite> default to &#8216;in&#8217; and &#8216;out&#8217; respectively, and a PUB socket
is most logical for the monitor socket, since it will never receive messages, and the
in/out prefix is well suited to the PUB/SUB topic subscription model. All messages sent on
<cite>mons</cite> will be multipart, the first part being the prefix corresponding to the socket that
received the message.</p>
<p>Or for launching an MQ in the background, there are <a class="reference internal" href="api/generated/zmq.devices.monitoredqueuedevice.html#zmq.devices.monitoredqueuedevice.ThreadMonitoredQueue" title="zmq.devices.monitoredqueuedevice.ThreadMonitoredQueue"><tt class="xref py py-class docutils literal"><span class="pre">ThreadMonitoredQueue</span></tt></a> and
<a class="reference internal" href="api/generated/zmq.devices.monitoredqueuedevice.html#zmq.devices.monitoredqueuedevice.ProcessMonitoredQueue" title="zmq.devices.monitoredqueuedevice.ProcessMonitoredQueue"><tt class="xref py py-class docutils literal"><span class="pre">ProcessMonitoredQueue</span></tt></a>, which function just like the base
BackgroundDevice objects, but add <tt class="xref py py-meth docutils literal"><span class="pre">foo_mon()</span></tt> methods for configuring the monitor socket.</p>
</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="eventloop.html" title="Eventloops and PyZMQ"
             >next</a> |</li>
        <li class="right" >
          <a href="serialization.html" title="Serializing messages with PyZMQ"
             >previous</a> |</li>
        <li><a href="index.html">home</a>|&nbsp;</li>
        <li><a href="search.html">search</a>|&nbsp;</li>
       <li><a href="api/index.html">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>