Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > c3732731228538f6126cae930c10ad71 > files > 202

python-pyro4-4.21-3.mga4.noarch.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>Tutorial &mdash; Pyro 4.21 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:     '4.21',
        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="Pyro 4.21 documentation" href="index.html" />
    <link rel="next" title="Command line tools" href="commandline.html" />
    <link rel="prev" title="Installing Pyro" href="install.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="commandline.html" title="Command line tools"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="install.html" title="Installing Pyro"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Pyro 4.21 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="tutorial">
<h1>Tutorial<a class="headerlink" href="#tutorial" title="Permalink to this headline">¶</a></h1>
<p>This tutorial will explain a couple of basic Pyro concepts,
a little bit about the name server, and you&#8217;ll learn to write a simple Pyro application.
You&#8217;ll do this by writing a warehouse system and a stock market simulator,
that demonstrate some key Pyro techniques.</p>
<div class="section" id="warm-up">
<h2>Warm-up<a class="headerlink" href="#warm-up" title="Permalink to this headline">¶</a></h2>
<p>Before proceeding, you should install Pyro if you haven&#8217;t done so. For instructions about that, see <a class="reference internal" href="install.html"><em>Installing Pyro</em></a>.</p>
<p>In this tutorial, you will use Pyro&#8217;s default configuration settings, so once Pyro is installed, you&#8217;re all set!
All you need is a text editor and a couple of console windows.
During the tutorial, you are supposed to run everything on a single machine.
This avoids initial networking complexity.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">For security reasons, Pyro runs stuff on localhost by default.
If you want to access things from different machines, you&#8217;ll have to tell Pyro
to do that explicitly.
At the end is a small paragraph <a class="reference internal" href="#not-localhost"><em>Running it on different machines</em></a> that tells you
how you can run the various components on different machines.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The code of the two tutorial &#8216;projects&#8217; is included in the Pyro source archive.
Just installing Pyro won&#8217;t provide this.
If you don&#8217;t want to type all the code, you should extract the Pyro source archive
(<tt class="file docutils literal"><span class="pre">Pyro4-X.Y.tar.gz</span></tt>) somewhere. You will then have an <tt class="file docutils literal"><span class="pre">examples</span></tt> directory
that contains a truckload of examples, including the two tutorial projects we will
be creating later in this tutorial, <tt class="file docutils literal"><span class="pre">warehouse</span></tt> and <tt class="file docutils literal"><span class="pre">stockquotes</span></tt>.
(There is more in there as well: the <tt class="file docutils literal"><span class="pre">tests</span></tt> directory contains the test suite
with all the unittests for Pyro&#8217;s code base.)</p>
</div>
</div>
<div class="section" id="pyro-concepts-and-tools">
<h2>Pyro concepts and tools<a class="headerlink" href="#pyro-concepts-and-tools" title="Permalink to this headline">¶</a></h2>
<p>Pyro enables code to call methods on objects even if that object is running on a remote machine:</p>
<div class="highlight-python"><pre>+----------+                         +----------+
| server A |                         | server B |
|          |       &lt; network &gt;       |          |
| Python   |                         |   Python |
| OBJECT ----------foo.invoke()--------&gt; OBJECT |
|          |                         |     foo  |
+----------+                         +----------+</pre>
</div>
<p>Pyro is mainly used as a library in your code but it also has several supporting command line tools <a class="footnote-reference" href="#commandline" id="id1">[1]</a>.
We won&#8217;t explain every one of them here as you will only need the &#8220;name server&#8221; for this tutorial.</p>
<table class="docutils footnote" frame="void" id="commandline" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>Actually there are no scripts or command files included with Pyro right now.
The <a class="reference internal" href="commandline.html#command-line"><em>Command line tools</em></a> are invoked by starting their package directly using the <tt class="kbd docutils literal"><span class="pre">-m</span></tt> argument
of the Python interpreter.</td></tr>
</tbody>
</table>
<div class="section" id="key-concepts">
<span id="keyconcepts"></span><h3>Key concepts<a class="headerlink" href="#key-concepts" title="Permalink to this headline">¶</a></h3>
<p>Here are a couple of key concepts you encounter when using Pyro:</p>
<dl class="docutils">
<dt>Proxy</dt>
<dd>A proxy is a substitute object for &#8220;the real thing&#8221;.
It intercepts the method calls you would normally do on an object as if it was the actual object.
Pyro then performs some magic to transfer the call to the computer that contains the <em>real</em> object,
where the actual method call is done, and the results are returned to the caller.
This means the calling code doesn&#8217;t have to know if it&#8217;s dealing with a normal or a remote object,
because the code is identical.
The class implementing Pyro proxies is <a class="reference internal" href="api/core.html#Pyro4.core.Proxy" title="Pyro4.core.Proxy"><tt class="xref py py-class docutils literal"><span class="pre">Pyro4.core.Proxy</span></tt></a></dd>
<dt><abbr title="Unique resource identifier">URI</abbr></dt>
<dd>This is what Pyro uses to identify every object.
(similar to what a web page URL is to point to the different documents on the web).
Its string form is like this: &#8220;PYRO:&#8221; + object name + &#8220;&#64;&#8221; + server name + port number.
The class implementing Pyro uris is <a class="reference internal" href="api/core.html#Pyro4.core.URI" title="Pyro4.core.URI"><tt class="xref py py-class docutils literal"><span class="pre">Pyro4.core.URI</span></tt></a></dd>
<dt>Pyro object</dt>
<dd>This is a normal Python object but it is registered with Pyro so that you can access it remotely.
Pyro objects are written just as any other object but the fact that Pyro knows something about
them makes them special, in the way that you can call methods on them from other programs.</dd>
<dt>Pyro daemon (server)</dt>
<dd>This is the part of Pyro that listens for remote method calls, dispatches them
to the appropriate actual objects, and returns the results to the caller.
All Pyro objects are registered in one or more daemons.</dd>
<dt>Pyro name server</dt>
<dd>The name server is a utility that provides a phone book for Pyro applications: you use it to look up a &#8220;number&#8221; by a &#8220;name&#8221;.
The name in Pyro&#8217;s case is the logical name of a remote object. The number is the exact location where Pyro can contact the object.
Usually there is just <em>one</em> name server running in your network.</dd>
<dt>Serialization</dt>
<dd>This is the process of transforming objects into streams of bytes that can be transported
over the network. The receiver deserializes them back into actual objects. Pyro needs to do
this with all the data that is passed as arguments to remote method calls, and their response
data. Not all objects can be serialized, so it is possible that passing a certain object to
Pyro won&#8217;t work even though a normal method call would accept it just fine.</dd>
</dl>
</div>
<div class="section" id="starting-a-name-server">
<h3>Starting a name server<a class="headerlink" href="#starting-a-name-server" title="Permalink to this headline">¶</a></h3>
<p>While the use of the Pyro name server is optional, we will use it in this tutorial.
It also shows a few basic Pyro concepts, so let us begin by explaining a little about it.
Open a console window and execute the following command to start a name server:</p>
<p><strong class="command">python -Wignore -m Pyro4.naming</strong></p>
<p>The <tt class="kbd docutils literal"><span class="pre">-Wignore</span></tt> is added to suppress a Pyro warning that we&#8217;re not interested in right now.
The name server will start and it prints something like:</p>
<div class="highlight-python"><pre>Not starting broadcast server for localhost.
NS running on localhost:9090 (127.0.0.1)
URI = PYRO:Pyro.NameServer@localhost:9090</pre>
</div>
<div class="sidebar">
<p class="first sidebar-title">Localhost</p>
<p class="last">By default, Pyro uses <em>localhost</em> to run stuff on, so you can&#8217;t by mistake expose your system to the outside world.
You&#8217;ll need to tell Pyro explicitly to use something else than <em>localhost</em>. But it is fine for the tutorial,
so we leave it as it is.</p>
</div>
<p>The name server has started and is listening on <em>localhost port 9090</em>.</p>
<p>It also printed an <abbr title="unique resource identifier">URI</abbr>. Remember that this is
what Pyro uses to identify every object.</p>
<p>The name server can be stopped with a <tt class="kbd docutils literal"><span class="pre">control-c</span></tt>, or on Windows, with <tt class="kbd docutils literal"><span class="pre">ctrl-break</span></tt>. But let it run
in the background for the rest of this tutorial.</p>
</div>
<div class="section" id="interacting-with-the-name-server">
<h3>Interacting with the name server<a class="headerlink" href="#interacting-with-the-name-server" title="Permalink to this headline">¶</a></h3>
<p>There&#8217;s another command line tool that let you interact with the name server: &#8220;nsc&#8221; (name server control tool).
You can use it, amongst other things, to see what all known registered objects in the naming server are.
Let&#8217;s do that right now. Type:</p>
<p><strong class="command">python -Wignore -m Pyro4.nsc list</strong></p>
<p>(the <tt class="kbd docutils literal"><span class="pre">-Wignore</span></tt> again is to suppress a warning) and it will print something like this:</p>
<div class="highlight-python"><pre>--------START LIST
Pyro.NameServer --&gt; PYRO:Pyro.NameServer@localhost:9090
--------END LIST</pre>
</div>
<p>The only object that is currently registered, is the name server itself! (Yes, the name server is a Pyro object
itself. Pyro and the &#8220;nsc&#8221; tool are using Pyro to talk to it).</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">As you can see, the name <tt class="docutils literal"><span class="pre">Pyro.NameServer</span></tt> is registered to point to the URI that we saw earlier.
This is mainly for completeness sake, and is not often used, because there are different ways to get
to talk to the name server (see below).</p>
</div>
<div class="sidebar">
<p class="first sidebar-title">The NameServer object</p>
<p class="last">The name server itself is a normal Pyro object which means the &#8216;nsc&#8217; tool, and any other code that talks to it,
is just using normal Pyro methods. The only &#8220;trickery&#8221; that makes it a bit different from other Pyro servers
is perhaps the broadcast responder, and the two command line tools to interact with it (<tt class="docutils literal"><span class="pre">Pyro4.naming</span></tt> and
<tt class="docutils literal"><span class="pre">Pyro4.nsc</span></tt>)</p>
</div>
<p>This is cool, but there&#8217;s a little detail left unexplained: <em>How did the nsc tool know where the name server was?</em>
Pyro has a couple of tactics to locate a name server.  The nsc tool uses them too:
Pyro uses a network broadcast to see if there&#8217;s a name server available somewhere (the name server contains
a broadcast responder that will respond &#8220;Yeah hi I&#8217;m here&#8221;).  So in many cases you won&#8217;t have to configure anything
to be able to discover the name server. If nobody answers though, Pyro tries the configured default or custom location.
If still nobody answers it prints a sad message and exits.
However if it found the name server, it is then possible to talk to it and get the location of any other registered object.
. This means that you won&#8217;t have to hard code any object locations in your code,
and that the code is capable of dynamically discovering everything at runtime.</p>
<p><em>But enough of that.</em> We need to start looking at how to actually write some code ourselves that uses Pyro!</p>
</div>
</div>
<div class="section" id="building-a-warehouse">
<h2>Building a Warehouse<a class="headerlink" href="#building-a-warehouse" title="Permalink to this headline">¶</a></h2>
<div class="admonition hint">
<p class="first admonition-title">Hint</p>
<p class="last">All code of this part of the tutorial can be found in the <tt class="file docutils literal"><span class="pre">examples/warehouse</span></tt> directory.</p>
</div>
<p>You&#8217;ll build build a simple warehouse that stores items, and that everyone can visit.
Visitors can store items and retrieve other items from the warehouse (if they&#8217;ve been stored there).</p>
<p>In this tutorial you&#8217;ll first write a normal Python program that more or less implements the complete warehouse system,
but in vanilla Python code. After that you&#8217;ll add Pyro support to it, to make it a distributed warehouse system,
where you can visit the central warehouse from many different computers.</p>
<div class="section" id="phase-1-a-simple-prototype">
<h3>phase 1: a simple prototype<a class="headerlink" href="#phase-1-a-simple-prototype" title="Permalink to this headline">¶</a></h3>
<p>To start with, write the vanilla Python code for the warehouse and its visitors.
This prototype is fully working but everything is running in a single process. It contains no Pyro
code at all, but shows what the system is going to look like later on.</p>
<p>The <tt class="docutils literal"><span class="pre">Warehouse</span></tt> object simply stores an array of items which we can query, and allows for a person
to take an item or to store an item. Here is the code (<tt class="file docutils literal"><span class="pre">warehouse.py</span></tt>):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>

<span class="k">class</span> <span class="nc">Warehouse</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">contents</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;chair&quot;</span><span class="p">,</span> <span class="s">&quot;bike&quot;</span><span class="p">,</span> <span class="s">&quot;flashlight&quot;</span><span class="p">,</span> <span class="s">&quot;laptop&quot;</span><span class="p">,</span> <span class="s">&quot;couch&quot;</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">list_contents</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">contents</span>

    <span class="k">def</span> <span class="nf">take</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">item</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">contents</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
        <span class="k">print</span><span class="p">(</span><span class="s">&quot;{0} took the {1}.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">item</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">store</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">item</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">contents</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
        <span class="k">print</span><span class="p">(</span><span class="s">&quot;{0} stored the {1}.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">item</span><span class="p">))</span>
</pre></div>
</div>
<p>Then there is a <tt class="docutils literal"><span class="pre">Person</span></tt> that can visit the warehouse. The person has a name and deposit and retrieve actions
on a particular warehouse. Here is the code (<tt class="file docutils literal"><span class="pre">person.py</span></tt>):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>
<span class="kn">import</span> <span class="nn">sys</span>

<span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span> <span class="o">&lt;</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">0</span><span class="p">):</span>
    <span class="nb">input</span> <span class="o">=</span> <span class="nb">raw_input</span>


<span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
    <span class="k">def</span> <span class="nf">visit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">warehouse</span><span class="p">):</span>
        <span class="k">print</span><span class="p">(</span><span class="s">&quot;This is {0}.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">deposit</span><span class="p">(</span><span class="n">warehouse</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">retrieve</span><span class="p">(</span><span class="n">warehouse</span><span class="p">)</span>
        <span class="k">print</span><span class="p">(</span><span class="s">&quot;Thank you, come again!&quot;</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">deposit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">warehouse</span><span class="p">):</span>
        <span class="k">print</span><span class="p">(</span><span class="s">&quot;The warehouse contains:&quot;</span><span class="p">,</span> <span class="n">warehouse</span><span class="o">.</span><span class="n">list_contents</span><span class="p">())</span>
        <span class="n">item</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s">&quot;Type a thing you want to store (or empty): &quot;</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">item</span><span class="p">:</span>
            <span class="n">warehouse</span><span class="o">.</span><span class="n">store</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">item</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">retrieve</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">warehouse</span><span class="p">):</span>
        <span class="k">print</span><span class="p">(</span><span class="s">&quot;The warehouse contains:&quot;</span><span class="p">,</span> <span class="n">warehouse</span><span class="o">.</span><span class="n">list_contents</span><span class="p">())</span>
        <span class="n">item</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s">&quot;Type something you want to take (or empty): &quot;</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">item</span><span class="p">:</span>
            <span class="n">warehouse</span><span class="o">.</span><span class="n">take</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">item</span><span class="p">)</span>
</pre></div>
</div>
<p>Finally you need a small script that actually runs the code. It creates the warehouse and two visitors, and
makes the visitors perform their actions in the warehouse. Here is the code (<tt class="file docutils literal"><span class="pre">visit.py</span></tt>):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># This is the code that runs this example.</span>
<span class="kn">from</span> <span class="nn">warehouse</span> <span class="kn">import</span> <span class="n">Warehouse</span>
<span class="kn">from</span> <span class="nn">person</span> <span class="kn">import</span> <span class="n">Person</span>

<span class="n">warehouse</span> <span class="o">=</span> <span class="n">Warehouse</span><span class="p">()</span>
<span class="n">janet</span> <span class="o">=</span> <span class="n">Person</span><span class="p">(</span><span class="s">&quot;Janet&quot;</span><span class="p">)</span>
<span class="n">henry</span> <span class="o">=</span> <span class="n">Person</span><span class="p">(</span><span class="s">&quot;Henry&quot;</span><span class="p">)</span>
<span class="n">janet</span><span class="o">.</span><span class="n">visit</span><span class="p">(</span><span class="n">warehouse</span><span class="p">)</span>
<span class="n">henry</span><span class="o">.</span><span class="n">visit</span><span class="p">(</span><span class="n">warehouse</span><span class="p">)</span>
</pre></div>
</div>
<p>Run this simple program. It will output something like this:</p>
<div class="highlight-python"><pre>$ python visit.py
This is Janet.
The warehouse contains: ['chair', 'bike', 'flashlight', 'laptop', 'couch']
Type a thing you want to store (or empty): television   # typed in
Janet stored the television.
The warehouse contains: ['chair', 'bike', 'flashlight', 'laptop', 'couch', 'television']
Type something you want to take (or empty): couch    # &lt;-- typed in
Janet took the couch.
Thank you, come again!
This is Henry.
The warehouse contains: ['chair', 'bike', 'flashlight', 'laptop', 'television']
Type a thing you want to store (or empty): bricks   # &lt;-- typed in
Henry stored the bricks.
The warehouse contains: ['chair', 'bike', 'flashlight', 'laptop', 'television', 'bricks']
Type something you want to take (or empty): bike   # &lt;-- typed in
Henry took the bike.
Thank you, come again!</pre>
</div>
</div>
<div class="section" id="phase-2-first-pyro-version">
<h3>phase 2: first Pyro version<a class="headerlink" href="#phase-2-first-pyro-version" title="Permalink to this headline">¶</a></h3>
<p>That wasn&#8217;t very exciting but you now have working code for the basics of the warehouse system.
Now you&#8217;ll use Pyro to turn the warehouse into a standalone component, that people from other
computers can visit. You&#8217;ll need to add a couple of lines to the <tt class="file docutils literal"><span class="pre">warehouse.py</span></tt> file so that it will
start a Pyro server for the warehouse object. The easiest way to do this is to create the object
that you want to make available as Pyro object, and register it with a &#8216;Pyro daemon&#8217; (the server that
listens for and processes incoming remote method calls):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">warehouse</span> <span class="o">=</span> <span class="n">Warehouse</span><span class="p">()</span>
<span class="n">Pyro4</span><span class="o">.</span><span class="n">Daemon</span><span class="o">.</span><span class="n">serveSimple</span><span class="p">(</span>
        <span class="p">{</span>
            <span class="n">warehouse</span><span class="p">:</span> <span class="s">&quot;example.warehouse&quot;</span>
        <span class="p">},</span>
        <span class="n">ns</span> <span class="o">=</span> <span class="bp">False</span><span class="p">)</span>
</pre></div>
</div>
<p>You&#8217;ll also need to add a little <tt class="docutils literal"><span class="pre">main</span></tt> function so it will be started correctly, which should
make the code now look like this (<tt class="file docutils literal"><span class="pre">warehouse.py</span></tt>):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>
<span class="kn">import</span> <span class="nn">Pyro4</span>
<span class="kn">import</span> <span class="nn">person</span>


<span class="k">class</span> <span class="nc">Warehouse</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">contents</span> <span class="o">=</span> <span class="p">[</span><span class="s">&quot;chair&quot;</span><span class="p">,</span> <span class="s">&quot;bike&quot;</span><span class="p">,</span> <span class="s">&quot;flashlight&quot;</span><span class="p">,</span> <span class="s">&quot;laptop&quot;</span><span class="p">,</span> <span class="s">&quot;couch&quot;</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">list_contents</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">contents</span>

    <span class="k">def</span> <span class="nf">take</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">item</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">contents</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
        <span class="k">print</span><span class="p">(</span><span class="s">&quot;{0} took the {1}.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">item</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">store</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">item</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">contents</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
        <span class="k">print</span><span class="p">(</span><span class="s">&quot;{0} stored the {1}.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">item</span><span class="p">))</span>


<span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
    <span class="n">warehouse</span> <span class="o">=</span> <span class="n">Warehouse</span><span class="p">()</span>
    <span class="n">Pyro4</span><span class="o">.</span><span class="n">Daemon</span><span class="o">.</span><span class="n">serveSimple</span><span class="p">(</span>
            <span class="p">{</span>
                <span class="n">warehouse</span><span class="p">:</span> <span class="s">&quot;example.warehouse&quot;</span>
            <span class="p">},</span>
            <span class="n">ns</span> <span class="o">=</span> <span class="bp">False</span><span class="p">)</span>

<span class="k">if</span> <span class="n">__name__</span><span class="o">==</span><span class="s">&quot;__main__&quot;</span><span class="p">:</span>
    <span class="n">main</span><span class="p">()</span>
</pre></div>
</div>
<p>Start the warehouse in a new console window, it will print something like this:</p>
<div class="highlight-python"><pre>$ python warehouse.py
Object &lt;__main__.Warehouse object at 0x025F4FF0&gt;:
    uri = PYRO:example.warehouse@localhost:51279
Pyro daemon running.</pre>
</div>
<p>It will become clear what you need to do with this output in a second.
You now need to slightly change the <tt class="file docutils literal"><span class="pre">visit.py</span></tt> script that runs the thing. Instead of creating a warehouse
directly and letting the persons visit that, it is going to use Pyro to connect to the stand alone warehouse
object that you started above. It needs to know the location of the warehouse object before
it can connect to it. This is the <strong>uri</strong> that is printed by the warehouse program above (<tt class="docutils literal"><span class="pre">PYRO:example.warehouse&#64;localhost:51279</span></tt>).
You&#8217;ll need to ask the user to enter that uri string into the program, and use Pyro to
create a <cite>proxy</cite> to the remote object:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">uri</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s">&quot;Enter the uri of the warehouse: &quot;</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
<span class="n">warehouse</span> <span class="o">=</span> <span class="n">Pyro4</span><span class="o">.</span><span class="n">Proxy</span><span class="p">(</span><span class="n">uri</span><span class="p">)</span>
</pre></div>
</div>
<p>That is all you need to change. Pyro will transparently forward the calls you make on the
warehouse object to the remote object, and return the results to your code. So the code will now look like this (<tt class="file docutils literal"><span class="pre">visit.py</span></tt>):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># This is the code that visits the warehouse.</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">Pyro4</span>
<span class="kn">from</span> <span class="nn">person</span> <span class="kn">import</span> <span class="n">Person</span>

<span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span><span class="o">&lt;</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">0</span><span class="p">):</span>
    <span class="nb">input</span> <span class="o">=</span> <span class="nb">raw_input</span>

<span class="n">uri</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s">&quot;Enter the uri of the warehouse: &quot;</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
<span class="n">warehouse</span> <span class="o">=</span> <span class="n">Pyro4</span><span class="o">.</span><span class="n">Proxy</span><span class="p">(</span><span class="n">uri</span><span class="p">)</span>
<span class="n">janet</span> <span class="o">=</span> <span class="n">Person</span><span class="p">(</span><span class="s">&quot;Janet&quot;</span><span class="p">)</span>
<span class="n">henry</span> <span class="o">=</span> <span class="n">Person</span><span class="p">(</span><span class="s">&quot;Henry&quot;</span><span class="p">)</span>
<span class="n">janet</span><span class="o">.</span><span class="n">visit</span><span class="p">(</span><span class="n">warehouse</span><span class="p">)</span>
<span class="n">henry</span><span class="o">.</span><span class="n">visit</span><span class="p">(</span><span class="n">warehouse</span><span class="p">)</span>
</pre></div>
</div>
<p>Notice that the code of <tt class="docutils literal"><span class="pre">Warehouse</span></tt> and <tt class="docutils literal"><span class="pre">Person</span></tt> classes didn&#8217;t change <em>at all</em>.</p>
<p>Run the program. It will output something like this:</p>
<div class="highlight-python"><pre>$ python visit.py
Enter the uri of the warehouse: PYRO:example.warehouse@localhost:51279  # copied from warehouse output
This is Janet.
The warehouse contains: ['chair', 'bike', 'flashlight', 'laptop', 'couch']
Type a thing you want to store (or empty): television   # typed in
The warehouse contains: ['chair', 'bike', 'flashlight', 'laptop', 'couch', 'television']
Type something you want to take (or empty): couch   # &lt;-- typed in
Thank you, come again!
This is Henry.
The warehouse contains: ['chair', 'bike', 'flashlight', 'laptop', 'television']
Type a thing you want to store (or empty): bricks   # &lt;-- typed in
The warehouse contains: ['chair', 'bike', 'flashlight', 'laptop', 'television', 'bricks']
Type something you want to take (or empty): bike    # &lt;-- typed in
Thank you, come again!</pre>
</div>
<p>And notice that in the other console window, where the warehouse server is running, the following is printed:</p>
<div class="highlight-python"><pre>Janet stored the television.
Janet took the couch.
Henry stored the bricks.
Henry took the bike.</pre>
</div>
</div>
<div class="section" id="phase-3-final-pyro-version">
<h3>phase 3: final Pyro version<a class="headerlink" href="#phase-3-final-pyro-version" title="Permalink to this headline">¶</a></h3>
<p>The code from the previous phase works fine and could be considered to be the final program,
but is a bit cumbersome because you need to copy-paste the warehouse URI all the time to be able to use it.
You will simplify it a bit in this phase by using the Pyro name server.
Also, you will use the Pyro excepthook to print a nicer exception message
if anything goes wrong (by taking something from the warehouse that is not present! Try that now with the code
from phase 2. You will get a <tt class="docutils literal"><span class="pre">ValueError:</span> <span class="pre">list.remove(x):</span> <span class="pre">x</span> <span class="pre">not</span> <span class="pre">in</span> <span class="pre">list</span></tt> but with a not so useful stack trace).</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Once again you can leave code of the <tt class="docutils literal"><span class="pre">Warehouse</span></tt> and <tt class="docutils literal"><span class="pre">Person</span></tt> classes <strong>unchanged</strong>. As you can see,
Pyro is not getting in your way at all here. You can often use it with only adding a couple of lines to your existing code.</p>
</div>
<p>Okay, stop the warehouse program from phase 2 if it is still running, and check if the name server
that you started in <a class="reference internal" href="#starting-a-name-server">Starting a name server</a> is still running in its own console window.</p>
<p>In <tt class="file docutils literal"><span class="pre">warehouse.py</span></tt> locate the statement <tt class="docutils literal"><span class="pre">Pyro4.Daemon.serveSimple(...</span></tt> and change the <tt class="docutils literal"><span class="pre">ns</span> <span class="pre">=</span> <span class="pre">False</span></tt> argument to <tt class="docutils literal"><span class="pre">ns</span> <span class="pre">=</span> <span class="pre">True</span></tt>.
This tells Pyro to use a name server to register the objects in.
(The <tt class="docutils literal"><span class="pre">Pyro4.Daemon.serveSimple</span></tt> is a very easy way to start a Pyro server but it provides very little control.
You will learn about another way of starting a server in <a class="reference internal" href="#building-a-stock-market-simulator">Building a Stock market simulator</a>).</p>
<p>In <tt class="file docutils literal"><span class="pre">visit.py</span></tt> remove the input statement that asks for the warehouse uri, and change the way the warehouse proxy
is created. Because you are now using a name server you can ask Pyro to locate the warehouse object automatically:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">warehouse</span> <span class="o">=</span> <span class="n">Pyro4</span><span class="o">.</span><span class="n">Proxy</span><span class="p">(</span><span class="s">&quot;PYRONAME:example.warehouse&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>Finally, install the <tt class="docutils literal"><span class="pre">Pyro4.util.excepthook</span></tt> as excepthook. You&#8217;ll soon see what this does to the exceptions and
stack traces your program produces when something goes wrong with a Pyro object.
So the code should look something like this (<tt class="file docutils literal"><span class="pre">visit.py</span></tt>):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># This is the code that visits the warehouse.</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">Pyro4</span>
<span class="kn">import</span> <span class="nn">Pyro4.util</span>
<span class="kn">from</span> <span class="nn">person</span> <span class="kn">import</span> <span class="n">Person</span>

<span class="n">sys</span><span class="o">.</span><span class="n">excepthook</span> <span class="o">=</span> <span class="n">Pyro4</span><span class="o">.</span><span class="n">util</span><span class="o">.</span><span class="n">excepthook</span>

<span class="n">warehouse</span> <span class="o">=</span> <span class="n">Pyro4</span><span class="o">.</span><span class="n">Proxy</span><span class="p">(</span><span class="s">&quot;PYRONAME:example.warehouse&quot;</span><span class="p">)</span>
<span class="n">janet</span> <span class="o">=</span> <span class="n">Person</span><span class="p">(</span><span class="s">&quot;Janet&quot;</span><span class="p">)</span>
<span class="n">henry</span> <span class="o">=</span> <span class="n">Person</span><span class="p">(</span><span class="s">&quot;Henry&quot;</span><span class="p">)</span>
<span class="n">janet</span><span class="o">.</span><span class="n">visit</span><span class="p">(</span><span class="n">warehouse</span><span class="p">)</span>
<span class="n">henry</span><span class="o">.</span><span class="n">visit</span><span class="p">(</span><span class="n">warehouse</span><span class="p">)</span>
</pre></div>
</div>
<p>Start the warehouse program again in a separate console window. It will print something like this:</p>
<div class="highlight-python"><pre>$ python warehouse.py
Object &lt;__main__.Warehouse object at 0x02496050&gt;:
    uri = PYRO:obj_426e82eea7534fb5bc78df0b5c0b6a04@localhost:51294
    name = example.warehouse
Pyro daemon running.</pre>
</div>
<p>As you can see the uri is different this time, it now contains some random id code instead of a name.
However it also printed an object name. This is the name that is now used in the name server for your warehouse
object. Check this with the &#8216;nsc&#8217; tool: <strong class="command">python -m Pyro4.nsc list</strong>, which will print something like:</p>
<div class="highlight-python"><pre>--------START LIST
Pyro.NameServer --&gt; PYRO:Pyro.NameServer@localhost:9090
example.warehouse --&gt; PYRO:obj_426e82eea7534fb5bc78df0b5c0b6a04@localhost:51294
--------END LIST</pre>
</div>
<p>This means you can now refer to that warehouse object using the name <tt class="docutils literal"><span class="pre">example.warehouse</span></tt> and Pyro will locate
the correct object for you automatically. This is what you changed in the <tt class="file docutils literal"><span class="pre">visit.py</span></tt> code so run that now
to see that it indeed works!</p>
<p><strong>Remote exception:</strong> You also installed Pyro&#8217;s custom excepthook so try that out. Run the <tt class="file docutils literal"><span class="pre">visit.py</span></tt> script
and try to take something from the warehouse that is not present (for instance, batteries):</p>
<div class="highlight-python"><pre>Type something you want to take (or empty): batteries
Traceback (most recent call last):
  File "visit.py", line 12, in &lt;module&gt;
    janet.visit(warehouse)
  File "d:\PROJECTS\Pyro4\examples\warehouse\phase3\person.py", line 14, in visit
    self.retrieve(warehouse)
  File "d:\PROJECTS\Pyro4\examples\warehouse\phase3\person.py", line 25, in retrieve
    warehouse.take(self.name, item)
  File "d:\PROJECTS\Pyro4\src\Pyro4\core.py", line 161, in __call__
    return self.__send(self.__name, args, kwargs)
  File "d:\PROJECTS\Pyro4\src\Pyro4\core.py", line 314, in _pyroInvoke
    raise data
ValueError: list.remove(x): x not in list
 +--- This exception occured remotely (Pyro) - Remote traceback:
 | Traceback (most recent call last):
 |   File "d:\PROJECTS\Pyro4\src\Pyro4\core.py", line 824, in handleRequest
 |     data=method(*vargs, **kwargs)   # this is the actual method call to the Pyro object
 |   File "warehouse.py", line 14, in take
 |     self.contents.remove(item)
 | ValueError: list.remove(x): x not in list
 +--- End of remote traceback</pre>
</div>
<p>What you can see now is that you not only get the usual exception traceback, <em>but also the exception
that occurred in the remote warehouse object on the server</em> (the &#8220;remote traceback&#8221;). This can greatly
help locating problems! As you can see it contains the source code lines from the warehouse code that
is running in the server, as opposed to the normal local traceback that only shows the remote method
call taking place inside Pyro...</p>
</div>
</div>
<div class="section" id="building-a-stock-market-simulator">
<h2>Building a Stock market simulator<a class="headerlink" href="#building-a-stock-market-simulator" title="Permalink to this headline">¶</a></h2>
<div class="admonition hint">
<p class="first admonition-title">Hint</p>
<p class="last">All of the code of this part of the tutorial can be found in the <tt class="file docutils literal"><span class="pre">examples/stockquotes</span></tt> directory.</p>
</div>
<p>You&#8217;ll build a simple stock quote system.
The idea is that we have multiple stock markets producing stock symbol
quotes. There is an aggregator that combines the quotes from all stock
markets. Finally there are multiple viewers that can register themselves
by the aggregator and let it know what stock symbols they&#8217;re interested in.
The viewers will then receive near-real-time stock quote updates for the
symbols they selected.  (Everything is fictional, of course):</p>
<table border="1" class="docutils">
<colgroup>
<col width="30%" />
<col width="14%" />
<col width="23%" />
<col width="14%" />
<col width="19%" />
</colgroup>
<tbody valign="top">
<tr class="row-odd"><td>Stockmarket 1</td>
<td>→</td>
<td>&nbsp;</td>
<td>→</td>
<td>Viewer 1</td>
</tr>
<tr class="row-even"><td>Stockmarket 2</td>
<td>→</td>
<td>Aggregator</td>
<td>→</td>
<td>Viewer 2</td>
</tr>
<tr class="row-odd"><td>...</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>...</td>
</tr>
<tr class="row-even"><td>Stockmarket N</td>
<td>→</td>
<td>&nbsp;</td>
<td>→</td>
<td>Viewer N</td>
</tr>
</tbody>
</table>
<div class="section" id="phase-1-simple-prototype">
<h3>phase 1: simple prototype<a class="headerlink" href="#phase-1-simple-prototype" title="Permalink to this headline">¶</a></h3>
<p>Again, like the previous application (the warehouse), you first create a working
version of the system by only using normal Python code.
This simple prototype will be functional but everything will be running in a single process.
It contains no Pyro code at all, but shows what the system is going to look like later on.</p>
<p>First create a file <tt class="file docutils literal"><span class="pre">stockmarket.py</span></tt> that will simulate a stock market that is producing
stock quotes for registered companies. You should be able to add a &#8216;listener&#8217; to it that will
be receiving stock quote updates. It should be able to report the stock symbols that are being
traded in this market as well. The code is as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># stockmarket.py</span>
<span class="kn">import</span> <span class="nn">random</span>

<span class="k">class</span> <span class="nc">StockMarket</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">marketname</span><span class="p">,</span> <span class="n">symbols</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">marketname</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">symbolmeans</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">symbol</span> <span class="ow">in</span> <span class="n">symbols</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">symbolmeans</span><span class="p">[</span><span class="n">symbol</span><span class="p">]</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="mi">200</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">aggregators</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">generate</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">quotes</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">symbol</span><span class="p">,</span> <span class="n">mean</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">symbolmeans</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">()</span> <span class="o">&lt;</span> <span class="mf">0.2</span><span class="p">:</span>
                <span class="n">quotes</span><span class="p">[</span><span class="n">symbol</span><span class="p">]</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">normalvariate</span><span class="p">(</span><span class="n">mean</span><span class="p">,</span> <span class="mi">20</span><span class="p">),</span> <span class="mi">2</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">aggregator</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">aggregators</span><span class="p">:</span>
            <span class="n">aggregator</span><span class="o">.</span><span class="n">quotes</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">quotes</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">listener</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">aggregator</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">aggregators</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">aggregator</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">symbols</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">symbolmeans</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
</pre></div>
</div>
<p>Then we need an <tt class="file docutils literal"><span class="pre">aggregator.py</span></tt> that combines all stock symbol quotes from all stockmarkets into one &#8216;stream&#8217;
(this is the object that will be the &#8216;listener&#8217; for the stock markets).
It should be possible to register one or more &#8216;viewers&#8217; with the stock symbols that viewer is interested in, so
that every viewer is only receiving the stock symbols it wants. The code is like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># aggregator.py</span>
<span class="k">class</span> <span class="nc">Aggregator</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">viewers</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">symbols</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">add_symbols</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">symbols</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">symbols</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">symbols</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">available_symbols</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">symbols</span>

    <span class="k">def</span> <span class="nf">view</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">viewer</span><span class="p">,</span> <span class="n">symbols</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">viewers</span><span class="p">[</span><span class="n">viewer</span><span class="p">]</span> <span class="o">=</span> <span class="n">symbols</span>

    <span class="k">def</span> <span class="nf">quotes</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">market</span><span class="p">,</span> <span class="n">stockquotes</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">symbol</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">stockquotes</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">for</span> <span class="n">viewer</span><span class="p">,</span> <span class="n">symbols</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">viewers</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="k">if</span> <span class="n">symbol</span> <span class="ow">in</span> <span class="n">symbols</span><span class="p">:</span>
                    <span class="n">viewer</span><span class="o">.</span><span class="n">quote</span><span class="p">(</span><span class="n">market</span><span class="p">,</span> <span class="n">symbol</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
</pre></div>
</div>
<p>The <tt class="file docutils literal"><span class="pre">viewer.py</span></tt> itself is an extremely simple object that just prints out the stock symbol quotes it receives:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># viewer.py</span>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>

<span class="k">class</span> <span class="nc">Viewer</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">quote</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">market</span><span class="p">,</span> <span class="n">symbol</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">print</span><span class="p">(</span><span class="s">&quot;{0}.{1}: {2}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">market</span><span class="p">,</span> <span class="n">symbol</span><span class="p">,</span> <span class="n">value</span><span class="p">))</span>
</pre></div>
</div>
<p>Finally you need to write a <tt class="file docutils literal"><span class="pre">main.py</span></tt> that imports the above modules, creates all objects, creates a few
companies that are traded on the market, connects them together, and contains a loop that drives the stock market quote generation.
Because we are not yet using Pyro here, it just creates two <tt class="docutils literal"><span class="pre">Stockmarket</span></tt> objects (with a name and the companies being traded).
A single <tt class="docutils literal"><span class="pre">Aggregator</span></tt> object is registered with both markets, to receive all updates.
A <tt class="docutils literal"><span class="pre">Viewer</span></tt> object is created and connected to the <tt class="docutils literal"><span class="pre">Aggregator</span></tt> with a few companies that the viewer wants to receive quotes from.
The code is like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># main.py</span>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">from</span> <span class="nn">stockmarket</span> <span class="kn">import</span> <span class="n">StockMarket</span>
<span class="kn">from</span> <span class="nn">aggregator</span> <span class="kn">import</span> <span class="n">Aggregator</span>
<span class="kn">from</span> <span class="nn">viewer</span> <span class="kn">import</span> <span class="n">Viewer</span>

<span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
    <span class="n">nasdaq</span> <span class="o">=</span> <span class="n">StockMarket</span><span class="p">(</span><span class="s">&quot;NASDAQ&quot;</span><span class="p">,</span> <span class="p">[</span><span class="s">&quot;AAPL&quot;</span><span class="p">,</span> <span class="s">&quot;CSCO&quot;</span><span class="p">,</span> <span class="s">&quot;MSFT&quot;</span><span class="p">,</span> <span class="s">&quot;GOOG&quot;</span><span class="p">])</span>
    <span class="n">newyork</span> <span class="o">=</span> <span class="n">StockMarket</span><span class="p">(</span><span class="s">&quot;NYSE&quot;</span><span class="p">,</span> <span class="p">[</span><span class="s">&quot;IBM&quot;</span><span class="p">,</span> <span class="s">&quot;HPQ&quot;</span><span class="p">,</span> <span class="s">&quot;BP&quot;</span><span class="p">])</span>

    <span class="n">agg</span> <span class="o">=</span> <span class="n">Aggregator</span><span class="p">()</span>
    <span class="n">agg</span><span class="o">.</span><span class="n">add_symbols</span><span class="p">(</span><span class="n">nasdaq</span><span class="o">.</span><span class="n">symbols</span><span class="p">())</span>
    <span class="n">agg</span><span class="o">.</span><span class="n">add_symbols</span><span class="p">(</span><span class="n">newyork</span><span class="o">.</span><span class="n">symbols</span><span class="p">())</span>
    <span class="k">print</span><span class="p">(</span><span class="s">&quot;aggregated symbols:&quot;</span><span class="p">,</span> <span class="n">agg</span><span class="o">.</span><span class="n">available_symbols</span><span class="p">())</span>

    <span class="n">nasdaq</span><span class="o">.</span><span class="n">listener</span><span class="p">(</span><span class="n">agg</span><span class="p">)</span>
    <span class="n">newyork</span><span class="o">.</span><span class="n">listener</span><span class="p">(</span><span class="n">agg</span><span class="p">)</span>

    <span class="n">view</span> <span class="o">=</span> <span class="n">Viewer</span><span class="p">()</span>
    <span class="n">agg</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">view</span><span class="p">,</span> <span class="p">[</span><span class="s">&quot;IBM&quot;</span><span class="p">,</span> <span class="s">&quot;AAPL&quot;</span><span class="p">,</span> <span class="s">&quot;MSFT&quot;</span><span class="p">])</span>
    <span class="k">print</span><span class="p">(</span><span class="s">&quot;&quot;</span><span class="p">)</span>
    <span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
        <span class="n">nasdaq</span><span class="o">.</span><span class="n">generate</span><span class="p">()</span>
        <span class="n">newyork</span><span class="o">.</span><span class="n">generate</span><span class="p">()</span>
        <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.5</span><span class="p">)</span>

<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span>
    <span class="n">main</span><span class="p">()</span>
</pre></div>
</div>
<p>If you now run <tt class="file docutils literal"><span class="pre">main.py</span></tt> it will print a stream of stock symbol quote updates that are being generated by the two
stock markets (but only the few symbols that the viewer wants to see):</p>
<div class="highlight-python"><pre>$ python main.py
aggregated symbols: ['GOOG', 'AAPL', 'CSCO', 'MSFT', 'HPQ', 'BP', 'IBM']

NYSE.IBM: 74.31
NYSE.IBM: 108.68
NASDAQ.AAPL: 64.17
NYSE.IBM: 83.19
NYSE.IBM: 92.5
NASDAQ.AAPL: 63.09
NASDAQ.MSFT: 161.3
....</pre>
</div>
</div>
<div class="section" id="phase-2-separation">
<h3>phase 2: separation<a class="headerlink" href="#phase-2-separation" title="Permalink to this headline">¶</a></h3>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">For brevity, the code for this phase of the stockquote tutorial is not shown. If you want to see it,
have a look at the <tt class="file docutils literal"><span class="pre">stockquotes</span></tt> example, <tt class="file docutils literal"><span class="pre">phase2</span></tt>.</p>
</div>
<p>This phase still contains no Pyro code, but you can already make the three components more autonomous than they were in phase 1.
This step is optional, you can skip it and continue with <a class="reference internal" href="#phase-3-pyro-version">phase 3: Pyro version</a> below if you want.</p>
<p>In this phase, every component of our stock market system now has a main function that starts up the component and connects
it to the other component(s). As the stock market is the source of the data, you create a daemon thread in <tt class="file docutils literal"><span class="pre">stockmarket.py</span></tt> for it
so that all markets produces stock quote changes in the background.
<tt class="file docutils literal"><span class="pre">main.py</span></tt> is a lot simpler now: it only starts the various components and then sits to wait for an exit signal.</p>
<p>The idea in this phase is that you tweak the existing code a little to make it suitable to be split up in different,
autonomous components:</p>
<ul class="simple">
<li>it helps to add a few debug print or log statements so that you can see what is going on in each component</li>
<li>each component will need some form of a &#8216;main&#8217; or &#8216;startup&#8217; function to create and launch it</li>
<li>the main program just needs to make sure the components are started.</li>
</ul>
</div>
<div class="section" id="phase-3-pyro-version">
<h3>phase 3: Pyro version<a class="headerlink" href="#phase-3-pyro-version" title="Permalink to this headline">¶</a></h3>
<p>Finally you use Pyro to make the various components fully distributed. Pyro is used to make them talk to each other.
The actual code for each component class hasn&#8217;t changed since phase 1, it is just the plumbing that you need to write to
glue them together. Pyro is making this a matter of just a few lines of code that is Pyro-specific, the rest of the
code is needed anyway to start up and configure the system. To be able to see the final result, the code is listed
once more with comments on what changed with respect to the version in phase 1 (phase 2 is optional, it just makes
for an easier transition).</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This time we won&#8217;t be using <tt class="docutils literal"><span class="pre">serveSimple</span></tt> to publish the objects and start the Daemon.
Instead, a daemon is created manually, we register our own objects,
and start the request loop ourselves. This needs more code but gives you more control.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>You will see the following line appear in the code:</p>
<p><tt class="docutils literal"><span class="pre">Pyro4.config.SERIALIZER</span> <span class="pre">=</span> <span class="pre">'pickle'</span></tt></p>
<p class="last">For now, ignore the exact meaning of this particular setting. It is needed to get the stock market
tutorial running in the form presented here. Basically it enables Pyro to transfer actual Python objects
to remote calls, instead of only simple types such as lists and strings.
In other chapters the meaning of this setting will be explained in more detail.</p>
</div>
<div class="section" id="main">
<h4>main<a class="headerlink" href="#main" title="Permalink to this headline">¶</a></h4>
<p>There&#8217;s no <tt class="file docutils literal"><span class="pre">main.py</span></tt> anymore. This is because you now start every component by itself, in separate
console windows for instance. They run autonomously without the need of a &#8216;main&#8217; program to start it all up in one place.</p>
</div>
<div class="section" id="stockmarket">
<h4>stockmarket<a class="headerlink" href="#stockmarket" title="Permalink to this headline">¶</a></h4>
<p>The <tt class="file docutils literal"><span class="pre">stockmarket.py</span></tt> gained a few print statements to see what is going on while it is running.
<em>Important:</em> there is <em>a single</em> change in the code to make it work with Pyro. Because Pyro needs to transfer
objects over the network, it requires those objects to be serializable. The <tt class="docutils literal"><span class="pre">symbols</span></tt> method returned the <tt class="docutils literal"><span class="pre">keys()</span></tt>
of the dictionary of symbols in the stockmarket. While this is a normal list in Python 2, it is a <tt class="docutils literal"><span class="pre">dict_keys</span></tt> object
in Python 3. These cannot be serialized (because it is a special iterator object). The simple solution is to
force the method to build a list and return that: <tt class="docutils literal"><span class="pre">list(dictionary.keys())</span></tt>.</p>
<p>It also gained a <tt class="docutils literal"><span class="pre">run</span></tt> method that will be running inside the background thread to generate stock quote updates.
The reason this needs to run in a thread is because the <tt class="docutils literal"><span class="pre">Stockmarket</span></tt> itself is also a Pyro object that must
listen to remote method calls (in this case, of the <tt class="docutils literal"><span class="pre">Aggregator</span></tt> object(s) that want to listen to it).
You can also choose to run the Pyro daemon loop in a background thread and generate stock quotes update in the main
thread, that doesn&#8217;t matter, as long as they run independently.</p>
<p>Finally it gained a <tt class="docutils literal"><span class="pre">main</span></tt> function to create a couple of stock markets as we did before.
This time however they&#8217;re registered as Pyro objects with the Pyro daemon. They&#8217;re also entered in the
name server as <tt class="docutils literal"><span class="pre">example.stockmarket.&lt;name&gt;</span></tt> so the <tt class="docutils literal"><span class="pre">Aggregator</span></tt> can find them easily.</p>
<p>The complete code for <tt class="file docutils literal"><span class="pre">stockmarket.py</span></tt> is now as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># stockmarket.py</span>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>
<span class="kn">import</span> <span class="nn">random</span>
<span class="kn">import</span> <span class="nn">threading</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">import</span> <span class="nn">Pyro4</span>

<span class="n">Pyro4</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">SERIALIZER</span> <span class="o">=</span> <span class="s">&#39;pickle&#39;</span>


<span class="k">class</span> <span class="nc">StockMarket</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">marketname</span><span class="p">,</span> <span class="n">symbols</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">marketname</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">symbolmeans</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">symbol</span> <span class="ow">in</span> <span class="n">symbols</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">symbolmeans</span><span class="p">[</span><span class="n">symbol</span><span class="p">]</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="mi">200</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">aggregators</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">generate</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">quotes</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">for</span> <span class="n">symbol</span><span class="p">,</span> <span class="n">mean</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">symbolmeans</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">()</span> <span class="o">&lt;</span> <span class="mf">0.2</span><span class="p">:</span>
                <span class="n">quotes</span><span class="p">[</span><span class="n">symbol</span><span class="p">]</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">normalvariate</span><span class="p">(</span><span class="n">mean</span><span class="p">,</span> <span class="mi">20</span><span class="p">),</span> <span class="mi">2</span><span class="p">)</span>
        <span class="k">print</span><span class="p">(</span><span class="s">&quot;new quotes generated for&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">aggregator</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">aggregators</span><span class="p">:</span>
            <span class="n">aggregator</span><span class="o">.</span><span class="n">quotes</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">quotes</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">listener</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">aggregator</span><span class="p">):</span>
        <span class="k">print</span><span class="p">(</span><span class="s">&quot;market {0} adding new aggregator&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">aggregators</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">aggregator</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">symbols</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">symbolmeans</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span>

    <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">def</span> <span class="nf">generate_symbols</span><span class="p">():</span>
            <span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
                <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">())</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">generate</span><span class="p">()</span>
        <span class="n">thread</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">generate_symbols</span><span class="p">)</span>
        <span class="n">thread</span><span class="o">.</span><span class="n">setDaemon</span><span class="p">(</span><span class="bp">True</span><span class="p">)</span>
        <span class="n">thread</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>


<span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
    <span class="n">nasdaq</span> <span class="o">=</span> <span class="n">StockMarket</span><span class="p">(</span><span class="s">&quot;NASDAQ&quot;</span><span class="p">,</span> <span class="p">[</span><span class="s">&quot;AAPL&quot;</span><span class="p">,</span> <span class="s">&quot;CSCO&quot;</span><span class="p">,</span> <span class="s">&quot;MSFT&quot;</span><span class="p">,</span> <span class="s">&quot;GOOG&quot;</span><span class="p">])</span>
    <span class="n">newyork</span> <span class="o">=</span> <span class="n">StockMarket</span><span class="p">(</span><span class="s">&quot;NYSE&quot;</span><span class="p">,</span> <span class="p">[</span><span class="s">&quot;IBM&quot;</span><span class="p">,</span> <span class="s">&quot;HPQ&quot;</span><span class="p">,</span> <span class="s">&quot;BP&quot;</span><span class="p">])</span>

    <span class="n">daemon</span> <span class="o">=</span> <span class="n">Pyro4</span><span class="o">.</span><span class="n">Daemon</span><span class="p">()</span>
    <span class="n">nasdaq_uri</span> <span class="o">=</span> <span class="n">daemon</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">nasdaq</span><span class="p">)</span>
    <span class="n">newyork_uri</span> <span class="o">=</span> <span class="n">daemon</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">newyork</span><span class="p">)</span>
    <span class="n">ns</span> <span class="o">=</span> <span class="n">Pyro4</span><span class="o">.</span><span class="n">locateNS</span><span class="p">()</span>
    <span class="n">ns</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s">&quot;example.stockmarket.nasdaq&quot;</span><span class="p">,</span> <span class="n">nasdaq_uri</span><span class="p">)</span>
    <span class="n">ns</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s">&quot;example.stockmarket.newyork&quot;</span><span class="p">,</span> <span class="n">newyork_uri</span><span class="p">)</span>

    <span class="n">nasdaq</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
    <span class="n">newyork</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
    <span class="k">print</span><span class="p">(</span><span class="s">&quot;Stockmarkets running.&quot;</span><span class="p">)</span>
    <span class="n">daemon</span><span class="o">.</span><span class="n">requestLoop</span><span class="p">()</span>

<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span>
    <span class="n">main</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="aggregator">
<h4>aggregator<a class="headerlink" href="#aggregator" title="Permalink to this headline">¶</a></h4>
<p>The <tt class="file docutils literal"><span class="pre">aggregator.py</span></tt> also gained a print function to be able to see in its console window what is going on
when a new viewer connects. The <tt class="docutils literal"><span class="pre">main</span></tt> function creates it, and connects it as a Pyro object to the Pyro daemon.
It also registers it with the name server as <tt class="docutils literal"><span class="pre">example.stockquote.aggregator</span></tt> so it can be easily retrieved by
any viewer that is interested.</p>
<p><em>How it connects to the available stock markets:</em> Remember that the stock market objects registered with the name server
using a name of the form <tt class="docutils literal"><span class="pre">example.stockmarket.&lt;name&gt;</span></tt>. It is possible to query the Pyro name server in such a way
that it returns a list of all objects matching a name pattern. This is exactly what the aggregator does, it asks
for all names starting with <tt class="docutils literal"><span class="pre">example.stockmarket.</span></tt> and for each of those, creates a Pyro proxy to that stock market.
It then registers itself as a listener with that remote stock market object.
Finally it starts the daemon loop to wait for incoming calls from any interested viewers.</p>
<p>The complete code for <tt class="file docutils literal"><span class="pre">aggregator.py</span></tt> is now as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># aggregator.py</span>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>
<span class="kn">import</span> <span class="nn">Pyro4</span>

<span class="n">Pyro4</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">SERIALIZER</span> <span class="o">=</span> <span class="s">&#39;pickle&#39;</span>


<span class="k">class</span> <span class="nc">Aggregator</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">viewers</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">symbols</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">add_symbols</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">symbols</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">symbols</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">symbols</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">available_symbols</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">symbols</span>

    <span class="k">def</span> <span class="nf">view</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">viewer</span><span class="p">,</span> <span class="n">symbols</span><span class="p">):</span>
        <span class="k">print</span><span class="p">(</span><span class="s">&quot;aggregator gets a new viewer, for symbols:&quot;</span><span class="p">,</span> <span class="n">symbols</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">viewers</span><span class="p">[</span><span class="n">viewer</span><span class="p">]</span> <span class="o">=</span> <span class="n">symbols</span>

    <span class="k">def</span> <span class="nf">quotes</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">market</span><span class="p">,</span> <span class="n">stockquotes</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">symbol</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">stockquotes</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">for</span> <span class="n">viewer</span><span class="p">,</span> <span class="n">symbols</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">viewers</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                <span class="k">if</span> <span class="n">symbol</span> <span class="ow">in</span> <span class="n">symbols</span><span class="p">:</span>
                    <span class="n">viewer</span><span class="o">.</span><span class="n">quote</span><span class="p">(</span><span class="n">market</span><span class="p">,</span> <span class="n">symbol</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
    <span class="n">aggregator</span> <span class="o">=</span> <span class="n">Aggregator</span><span class="p">()</span>
    <span class="n">daemon</span> <span class="o">=</span> <span class="n">Pyro4</span><span class="o">.</span><span class="n">Daemon</span><span class="p">()</span>
    <span class="n">agg_uri</span> <span class="o">=</span> <span class="n">daemon</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">aggregator</span><span class="p">)</span>
    <span class="n">ns</span> <span class="o">=</span> <span class="n">Pyro4</span><span class="o">.</span><span class="n">locateNS</span><span class="p">()</span>
    <span class="n">ns</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="s">&quot;example.stockquote.aggregator&quot;</span><span class="p">,</span> <span class="n">agg_uri</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">market</span><span class="p">,</span> <span class="n">market_uri</span> <span class="ow">in</span> <span class="n">ns</span><span class="o">.</span><span class="n">list</span><span class="p">(</span><span class="n">prefix</span><span class="o">=</span><span class="s">&quot;example.stockmarket.&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
        <span class="k">print</span><span class="p">(</span><span class="s">&quot;joining market&quot;</span><span class="p">,</span> <span class="n">market</span><span class="p">)</span>
        <span class="n">stockmarket</span> <span class="o">=</span> <span class="n">Pyro4</span><span class="o">.</span><span class="n">Proxy</span><span class="p">(</span><span class="n">market_uri</span><span class="p">)</span>
        <span class="n">stockmarket</span><span class="o">.</span><span class="n">listener</span><span class="p">(</span><span class="n">aggregator</span><span class="p">)</span>
        <span class="n">aggregator</span><span class="o">.</span><span class="n">add_symbols</span><span class="p">(</span><span class="n">stockmarket</span><span class="o">.</span><span class="n">symbols</span><span class="p">())</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">aggregator</span><span class="o">.</span><span class="n">available_symbols</span><span class="p">():</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;no symbols found! (have you started the stock market first?)&quot;</span><span class="p">)</span>
    <span class="k">print</span><span class="p">(</span><span class="s">&quot;Aggregator running. Symbols:&quot;</span><span class="p">,</span> <span class="n">aggregator</span><span class="o">.</span><span class="n">available_symbols</span><span class="p">())</span>
    <span class="n">daemon</span><span class="o">.</span><span class="n">requestLoop</span><span class="p">()</span>

<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span>
    <span class="n">main</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="viewer">
<h4>viewer<a class="headerlink" href="#viewer" title="Permalink to this headline">¶</a></h4>
<p>You don&#8217;t need to change the <tt class="docutils literal"><span class="pre">Viewer</span></tt> at all, besides the <tt class="docutils literal"><span class="pre">main</span></tt> function that needs to be added to start it up by itself.
It needs to create a viewer object and register it with a Pyro daemon to be able to receive stock quote update calls.
You can connect it to a running aggregator simply by asking Pyro to look that up in the name server. That can be done
by using the special <tt class="docutils literal"><span class="pre">PYRONAME:&lt;object</span> <span class="pre">name&gt;</span></tt> uri format. For the aggregator that would be: <tt class="docutils literal"><span class="pre">PYRONAME:example.stockquote.aggregator</span></tt>
(because <tt class="docutils literal"><span class="pre">example.stockquote.aggregator</span></tt> is the name the aggregator used to register itself with the name server).</p>
<p>It is also nice to ask the user for a list of stock symbols he is interested in so do that and register the
viewer with the aggregator, passing the list of entered stock symbols to filter on.</p>
<p>Finally start the daemon loop to wait for incoming calls. The code is as follows:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># viewer.py</span>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">Pyro4</span>

<span class="n">Pyro4</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">SERIALIZER</span> <span class="o">=</span> <span class="s">&#39;pickle&#39;</span>

<span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span> <span class="o">&lt;</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">0</span><span class="p">):</span>
    <span class="nb">input</span> <span class="o">=</span> <span class="nb">raw_input</span>


<span class="k">class</span> <span class="nc">Viewer</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">quote</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">market</span><span class="p">,</span> <span class="n">symbol</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">print</span><span class="p">(</span><span class="s">&quot;{0}.{1}: {2}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">market</span><span class="p">,</span> <span class="n">symbol</span><span class="p">,</span> <span class="n">value</span><span class="p">))</span>


<span class="k">def</span> <span class="nf">main</span><span class="p">():</span>
    <span class="n">viewer</span> <span class="o">=</span> <span class="n">Viewer</span><span class="p">()</span>
    <span class="n">daemon</span> <span class="o">=</span> <span class="n">Pyro4</span><span class="o">.</span><span class="n">Daemon</span><span class="p">()</span>
    <span class="n">daemon</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">viewer</span><span class="p">)</span>
    <span class="n">aggregator</span> <span class="o">=</span> <span class="n">Pyro4</span><span class="o">.</span><span class="n">Proxy</span><span class="p">(</span><span class="s">&quot;PYRONAME:example.stockquote.aggregator&quot;</span><span class="p">)</span>
    <span class="k">print</span><span class="p">(</span><span class="s">&quot;Available stock symbols:&quot;</span><span class="p">,</span> <span class="n">aggregator</span><span class="o">.</span><span class="n">available_symbols</span><span class="p">())</span>
    <span class="n">symbols</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s">&quot;Enter symbols you want to view (comma separated):&quot;</span><span class="p">)</span>
    <span class="n">symbols</span> <span class="o">=</span> <span class="p">[</span><span class="n">symbol</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="k">for</span> <span class="n">symbol</span> <span class="ow">in</span> <span class="n">symbols</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;,&quot;</span><span class="p">)]</span>
    <span class="n">aggregator</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">viewer</span><span class="p">,</span> <span class="n">symbols</span><span class="p">)</span>
    <span class="k">print</span><span class="p">(</span><span class="s">&quot;Viewer listening on symbols&quot;</span><span class="p">,</span> <span class="n">symbols</span><span class="p">)</span>
    <span class="n">daemon</span><span class="o">.</span><span class="n">requestLoop</span><span class="p">()</span>

<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span>
    <span class="n">main</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="running-the-final-program">
<h4>running the final program<a class="headerlink" href="#running-the-final-program" title="Permalink to this headline">¶</a></h4>
<p>To run the final stock quote system you need to do the following:</p>
<ul>
<li><p class="first">Open a new console window to start the name server in.</p>
</li>
<li><p class="first">Set the following environment variable:</p>
<div class="highlight-python"><pre>set PYRO_SERIALIZER=pickle      (windows)
export PYRO_SERIALIZER=pickle   (unix/mac/linux)</pre>
</div>
<p>Once more, ignore the exact meaning of this particular setting. It is needed to get the stock market
tutorial running in the form presented here. In other chapters the meaning of this setting will be explained.</p>
</li>
<li><p class="first">start the Pyro name server (<strong class="command">python -m Pyro4.naming</strong>) in this window.</p>
</li>
</ul>
<p>After that, start the following, each in a separate console window (so they run in parallel):</p>
<ul class="simple">
<li>start the stock market</li>
<li>start the aggregator</li>
<li>start one or more of the viewers.</li>
</ul>
<p>The output of the stock market looks like this:</p>
<div class="highlight-python"><pre>$ python stockmarket.py
Stockmarkets running.
new quotes generated for NASDAQ
new quotes generated for NASDAQ
new quotes generated for NYSE
new quotes generated for NASDAQ
...</pre>
</div>
<p>The output of the aggregator looks like this:</p>
<div class="highlight-python"><pre>$ python aggregator.py
joining market example.stockmarket.newyork
joining market example.stockmarket.nasdaq
Aggregator running. Symbols: ['HPQ', 'BP', 'IBM', 'GOOG', 'AAPL', 'CSCO', 'MSFT']
aggregator gets a new viewer, for symbols: ['GOOG', 'CSCO', 'BP']</pre>
</div>
<p>The output of the viewer looks like this:</p>
<div class="highlight-python"><pre>$ python viewer.py
Available stock symbols: ['HPQ', 'BP', 'IBM', 'GOOG', 'AAPL', 'CSCO', 'MSFT']
Enter symbols you want to view (comma separated):GOOG,CSCO,BP    # &lt;---- typed in
Viewer listening on symbols ['GOOG', 'CSCO', 'BP']
NYSE.BP: 88.96
NASDAQ.GOOG: -9.61
NYSE.BP: 113.8
NASDAQ.CSCO: 125.11
NYSE.BP: 77.43
NASDAQ.GOOG: 17.64
NASDAQ.CSCO: 157.21
NASDAQ.GOOG: 7.59
...</pre>
</div>
<p>If you&#8217;re interested to see what the name server now contains, type <strong class="command">python -m Pyro4.nsc list</strong>:</p>
<div class="highlight-python"><pre>$ python -m Pyro4.nsc list
--------START LIST
Pyro.NameServer --&gt; PYRO:Pyro.NameServer@localhost:9090
example.stockmarket.nasdaq --&gt; PYRO:obj_fc742f1656bd4c7e80bee17c33787147@localhost:50510
example.stockmarket.newyork --&gt; PYRO:obj_6bd09853979f4d13a73263e51a9c266b@localhost:50510
example.stockquote.aggregator --&gt; PYRO:obj_2c7a4f5341b1464c8cc6091f3997230f@localhost:50512
--------END LIST</pre>
</div>
</div>
</div>
</div>
<div class="section" id="running-it-on-different-machines">
<span id="not-localhost"></span><h2>Running it on different machines<a class="headerlink" href="#running-it-on-different-machines" title="Permalink to this headline">¶</a></h2>
<p>For security reasons, Pyro runs stuff on localhost by default.
If you want to access things from different machines, you&#8217;ll have to tell Pyro to do that explicitly.
This paragraph shows you how very briefly you can do this.
For more details, refer to the chapters in this manual about the relevant Pyro components.</p>
<dl class="docutils">
<dt><em>Name server</em></dt>
<dd>to start the nameserver in such a way that it is accessible from other machines,
start it with an appropriate -n argument, like this: <strong class="command">python -m Pyro4.naming -n your_hostname_here</strong></dd>
<dt><em>Warehouse server</em></dt>
<dd><p class="first">You&#8217;ll have to modify <tt class="file docutils literal"><span class="pre">warehouse.py</span></tt>. Right before the <tt class="docutils literal"><span class="pre">serveSimple</span></tt> call you have to tell it to bind the daemon on your hostname
instead of localhost. One way to do this is by setting the <tt class="docutils literal"><span class="pre">HOST</span></tt> config item:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">Pyro4</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">HOST</span> <span class="o">=</span> <span class="s">&quot;your_hostname_here&quot;</span>
<span class="n">Pyro4</span><span class="o">.</span><span class="n">Daemon</span><span class="o">.</span><span class="n">serveSimple</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
</pre></div>
</div>
<p>You can also choose to leave the code alone, and instead set the <tt class="docutils literal"><span class="pre">PYRO_HOST</span></tt> environment variable
before starting the warehouse server.
Another option is pass the required host (and perhaps even port) arguments to <tt class="docutils literal"><span class="pre">serveSimple</span></tt>:</p>
<div class="last highlight-python"><div class="highlight"><pre><span class="n">Pyro4</span><span class="o">.</span><span class="n">Daemon</span><span class="o">.</span><span class="n">serveSimple</span><span class="p">(</span>
        <span class="p">{</span>
            <span class="n">warehouse</span><span class="p">:</span> <span class="s">&quot;example.warehouse&quot;</span>
        <span class="p">},</span>
        <span class="n">host</span> <span class="o">=</span> <span class="s">&#39;your_hostname_here&#39;</span><span class="p">,</span>
        <span class="n">ns</span> <span class="o">=</span> <span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
</dd>
<dt><em>Stock market servers</em></dt>
<dd>This example already creates a daemon object instead of using the <tt class="docutils literal"><span class="pre">serveSimple</span></tt> call.
You&#8217;ll have to modify the three source files because they all create a daemon.
But you&#8217;ll only have to add the proper <tt class="docutils literal"><span class="pre">host</span></tt> argument to the construction of the Daemon,
to set it to your machine name instead of the default of localhost.
Ofcourse you could also change the <tt class="docutils literal"><span class="pre">HOST</span></tt> config item (either in the code itself,
or by setting the <tt class="docutils literal"><span class="pre">PYRO_HOST</span></tt> environment variable before launching.</dd>
</dl>
</div>
<div class="section" id="other-means-of-creating-connections">
<h2>Other means of creating connections<a class="headerlink" href="#other-means-of-creating-connections" title="Permalink to this headline">¶</a></h2>
<p>In both tutorials above we used the Name Server for easy object lookup.
The use of the name server is optional, see <a class="reference internal" href="nameserver.html#name-server"><em>Name Server</em></a> for details.
There are various other options for connecting your client code to your Pyro objects,
have a look at the client code details: <a class="reference internal" href="clientcode.html#object-discovery"><em>Object discovery</em></a>
and the server code details: <a class="reference internal" href="servercode.html#publish-objects"><em>Pyro Daemon: publishing Pyro objects</em></a>.</p>
</div>
<div class="section" id="ok-what-s-next">
<h2>Ok, what&#8217;s next?<a class="headerlink" href="#ok-what-s-next" title="Permalink to this headline">¶</a></h2>
<p><em>Congratulations!</em>  You completed the Pyro tutorials in which you built a simple warehouse storage system,
and a stock market simulation system consisting of various independent components that talk to each other using Pyro.
The Pyro distribution archive contains a truckload of example programs with short descriptions that you could
study to see how to use the various features that Pyro has to offer. Or just browse the manual for more detailed
information. Please consider joining the Pyro mailing list (see <a class="reference internal" href="index.html"><em>front page</em></a>). Happy remote object programming!</p>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="index.html">
              <img class="logo" src="_static/pyro.png" alt="Logo"/>
            </a></p>
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Tutorial</a><ul>
<li><a class="reference internal" href="#warm-up">Warm-up</a></li>
<li><a class="reference internal" href="#pyro-concepts-and-tools">Pyro concepts and tools</a><ul>
<li><a class="reference internal" href="#key-concepts">Key concepts</a></li>
<li><a class="reference internal" href="#starting-a-name-server">Starting a name server</a></li>
<li><a class="reference internal" href="#interacting-with-the-name-server">Interacting with the name server</a></li>
</ul>
</li>
<li><a class="reference internal" href="#building-a-warehouse">Building a Warehouse</a><ul>
<li><a class="reference internal" href="#phase-1-a-simple-prototype">phase 1: a simple prototype</a></li>
<li><a class="reference internal" href="#phase-2-first-pyro-version">phase 2: first Pyro version</a></li>
<li><a class="reference internal" href="#phase-3-final-pyro-version">phase 3: final Pyro version</a></li>
</ul>
</li>
<li><a class="reference internal" href="#building-a-stock-market-simulator">Building a Stock market simulator</a><ul>
<li><a class="reference internal" href="#phase-1-simple-prototype">phase 1: simple prototype</a></li>
<li><a class="reference internal" href="#phase-2-separation">phase 2: separation</a></li>
<li><a class="reference internal" href="#phase-3-pyro-version">phase 3: Pyro version</a><ul>
<li><a class="reference internal" href="#main">main</a></li>
<li><a class="reference internal" href="#stockmarket">stockmarket</a></li>
<li><a class="reference internal" href="#aggregator">aggregator</a></li>
<li><a class="reference internal" href="#viewer">viewer</a></li>
<li><a class="reference internal" href="#running-the-final-program">running the final program</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#running-it-on-different-machines">Running it on different machines</a></li>
<li><a class="reference internal" href="#other-means-of-creating-connections">Other means of creating connections</a></li>
<li><a class="reference internal" href="#ok-what-s-next">Ok, what&#8217;s next?</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="install.html"
                        title="previous chapter">Installing Pyro</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="commandline.html"
                        title="next chapter">Command line tools</a></p>
<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="commandline.html" title="Command line tools"
             >next</a> |</li>
        <li class="right" >
          <a href="install.html" title="Installing Pyro"
             >previous</a> |</li>
        <li><a href="index.html">Pyro 4.21 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright Irmen de Jong.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>