<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body bgcolor="#ffffff" text="#000000">
It would be really cool if an rpc call could return a generator.  I
know that there are a few reasons why one would not expect it to be
part of the library (e.g. the client would need to receive asynchronous
messages, and it's not part of the rpc standard), however below I show
a possible implementation, given that the client is also able to be a
server...<br>
<br>
I am aware of MultiCall, but that is actually something like the
inverse of what I want.  I need a consumer.<br>
<br>
Example:<br>
<hr size="2" width="100%">
<pre><tt># hypothetical client code (running at </tt><tt><span class="p"></span><span
 class="s"><a class="moz-txt-link-rfc2396E" href="http://rpc.myserver.com">"http://rpc.myserver.com"</a>)</span></tt><tt></tt>
<tt><span class="kn">
</span></tt><span class="k">from</span> <tt><span class="nn">fancyxmlrpc</span></tt> <span
 class="k">import</span> Fancy<span class="nn">XMLRPCServer</span>
<span class="n"></span><span class="k"></span><span class="n">
server</span> <span class="o">=</span> Fancy<span class="nn">XMLRPCServer</span><span
 class="p">((</span><span class="s">'localhost'</span><span class="p">,</span> <span
 class="mi">9000</span><span class="p">)</span><span class="bp"></span><span
 class="p">)</span>
<span class="c"></span><span class="p"></span>
<span class="kw1">def</span> primes<span class="br0">(</span><span
 class="br0">)</span>:
    n = <span class="nu0">2</span>
    p = <span class="br0">[</span><span class="br0">]</span>
    <span class="kw1">while</span> <span class="kw2">True</span>:
        <span class="kw1">if</span> <span class="kw1">not</span> any<span
 class="br0">(</span> n % f == <span class="nu0">0</span> <span
 class="kw1">for</span> f <span class="kw1">in</span> p <span
 class="br0">)</span>:<span class="co1"></span>
            <span class="kw1">yield</span> n
            p.<span class="me1">append</span><span class="br0">(</span> n <span
 class="br0">)</span>
        n += <span class="nu0">1</span>

<span class="n">server</span><span class="o">.</span><span class="n">register_generator</span><span
 class="p">(prime</span><span class="n">s</span><span class="p">)</span>
<span class="k"></span><span class="n">server</span><span class="o">.</span><span
 class="n">serve_forever</span><span class="p">()</span></pre>
<hr size="2" width="100%"><tt># hypothetical client code (running at </tt><tt><span
 class="n"><a class="moz-txt-link-freetext" href="http://www.mywebsite.com">http://www.mywebsite.com</a>)</span></tt><tt>:</tt><br>
<tt><span class="c"></span><span class="kn"><br>
from</span> <span class="nn">fancyxmlrpc</span> <span class="kn">import</span>
Fancy<span class="n">ServerProxy</span><span class="p"></span><span
 class="n"></span></tt><br>
<tt><span class="c"></span><span class="n"><br>
</span></tt><tt>server_url = <span class="p"></span><span class="s"><a class="moz-txt-link-rfc2396E" href="http://rpc.myserver.com">"http://rpc.myserver.com"</a></span></tt><br>
<tt><span class="n">local_url = <a class="moz-txt-link-rfc2396E" href="http://www.mywebsite.com">"http://www.mywebsite.com"</a><br>
<br>
server</span> <span class="o">=</span> <span class="n"></span></tt><tt>Fancy<span
 class="n"></span></tt><tt><span class="n">ServerProxy</span><span
 class="p">(</span></tt><tt>server_url</tt><tt><span class="p"></span><span
 class="s">, </span></tt><tt><span class="n">local_url)</span></tt><br>
<tt><span class="k"></span><span class="k"><br>
g =</span> <span class="n">server</span><span class="o">.</span><span
 class="n">examples</span><span class="o">.</span><span class="n">getStateNames</span><span
 class="p">(</span><span class="mf"></span><span class="p">)</span></tt><br>
<tt><br>
for x in g:<br>
</tt><tt>    print x<br>
</tt>
<hr size="2" width="100%"><br>
Okay, so to implement this, the trick would be to implement the
generator wrapper as a hidden xmlrpc conversation.  On the client side,
<tt><span class="n"></span></tt><tt>Fancy<span class="n"></span></tt><tt><span
 class="n">ServerProxy </span></tt><span class="n">would encapsulate a
hidden RPC server with a function that receives each item that is
yielded by the server and queues them while the client generator
consumes the queue and yields the items.<br>
<br>
The practical constraints of my specific application are:<br>
1. The rpc server is a highly specialized slave system that does heavy
duty work.<br>
</span><span class="n">2. The rpc client is itself a web server that
dispatches work requests to the rpc server(s) and displays the current
status of work done so far.<br>
3. The generators will typically run for a long time (hours) and yield
data periodically (perhaps once a minute).<br>
4. Trusted users will write generators on the server and consumers on
the client (web site) and use the web site to make requests.<br>
5. It would be even better if my generator yields numpy arrays rather
than lists.<br>
6. It would be nice to be able to scale to allow the web site to
dispatch to multiple work servers.<br>
<br>
</span><br>
<span class="k"></span>So my questions are:<br>
1. Does using xmlrpc make any sense for this?<br>
2. I am missing an easier way to do this?<br>
3. Any good examples of things like this?<br>
<br>
<br>
</body>
</html>