/usr/share/doc/pyro4-doc/html/clientcode.html is in pyro4-doc 4.63-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 | <!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>Clients: Calling remote objects — Pyro 4.63 documentation</title>
<link rel="stylesheet" href="_static/classic.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '4.63',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="/usr/share/javascript/jquery/jquery.js"></script>
<script type="text/javascript" src="/usr/share/javascript/underscore/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Servers: hosting Pyro objects" href="servercode.html" />
<link rel="prev" title="Command line tools" href="commandline.html" />
</head>
<body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="servercode.html" title="Servers: hosting Pyro objects"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="commandline.html" title="Command line tools"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">Pyro 4.63 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="clients-calling-remote-objects">
<span id="index-0"></span><h1>Clients: Calling remote objects<a class="headerlink" href="#clients-calling-remote-objects" title="Permalink to this headline">¶</a></h1>
<p>This chapter explains how you write code that calls remote objects.
Often, a program that calls methods on a Pyro object is called a <em>client</em> program.
(The program that provides the object and actually runs the methods, is the <em>server</em>.
Both roles can be mixed in a single program.)</p>
<p>Make sure you are familiar with Pyro’s <a class="reference internal" href="tutorials.html#keyconcepts"><span class="std std-ref">Key concepts</span></a> before reading on.</p>
<div class="section" id="object-discovery">
<span id="index-1"></span><span id="id1"></span><h2>Object discovery<a class="headerlink" href="#object-discovery" title="Permalink to this headline">¶</a></h2>
<p>To be able to call methods on a Pyro object, you have to tell Pyro where it can find
the actual object. This is done by creating an appropriate URI, which contains amongst
others the object name and the location where it can be found.
You can create it in a number of ways.</p>
<ul id="index-2">
<li><dl class="first docutils">
<dt>directly use the object name and location.</dt>
<dd><p class="first">This is the easiest way and you write an URI directly like this: <code class="docutils literal"><span class="pre">PYRO:someobjectid@servername:9999</span></code>
It requires that you already know the object id, servername, and port number.
You could choose to use fixed object names and fixed port numbers to connect Pyro daemons on.
For instance, you could decide that your music server object is always called “musicserver”,
and is accessible on port 9999 on your server musicbox.my.lan. You could then simply use:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">uri_string</span> <span class="o">=</span> <span class="s2">"PYRO:musicserver@musicbox.my.lan:9999"</span>
<span class="c1"># or use Pyro4.URI("...") for an URI object instead of a string</span>
</pre></div>
</div>
<p class="last">Most examples that come with Pyro simply ask the user to type this in on the command line,
based on what the server printed. This is not very useful for real programs,
but it is a simple way to make it work. You could write the information to a file
and read that from a file share (only slightly more useful, but it’s just an idea).</p>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt>use a logical name and look it up in the name server.</dt>
<dd><p class="first">A more flexible way of locating your objects is using logical names for them and storing
those in the Pyro name server. Remember that the name server is like a phone book, you look
up a name and it gives you the exact location.
To continue on the previous bullet, this means your clients would only have to know the
logical name “musicserver”. They can then use the name server to obtain the proper URI:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">Pyro4</span>
<span class="n">nameserver</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">uri</span> <span class="o">=</span> <span class="n">nameserver</span><span class="o">.</span><span class="n">lookup</span><span class="p">(</span><span class="s2">"musicserver"</span><span class="p">)</span>
<span class="c1"># ... uri now contains the URI with actual location of the musicserver object</span>
</pre></div>
</div>
<p class="last">You might wonder how Pyro finds the Name server. This is explained in the separate chapter <a class="reference internal" href="nameserver.html"><span class="doc">Name Server</span></a>.</p>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt>use a logical name and let Pyro look it up in the name server for you.</dt>
<dd><p class="first">Very similar to the option above, but even more convenient, is using the <em>meta</em>-protocol
identifier <code class="docutils literal"><span class="pre">PYRONAME</span></code> in your URI string. It lets Pyro know that it should lookup
the name following it, in the name server. Pyro should then
use the resulting URI from the name server to contact the actual object.
See <a class="reference internal" href="nameserver.html#nameserver-pyroname"><span class="std std-ref">The PYRONAME protocol type</span></a>.
This means you can write:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">uri_string</span> <span class="o">=</span> <span class="s2">"PYRONAME:musicserver"</span>
<span class="c1"># or Pyro4.URI("PYRONAME:musicserver") for an URI object</span>
</pre></div>
</div>
<p class="last">You can use this URI everywhere you would normally use a normal uri (using <code class="docutils literal"><span class="pre">PYRO</span></code>).
Everytime Pyro encounters the <code class="docutils literal"><span class="pre">PYRONAME</span></code> uri it will use the name server automatically
to look up the object for you. <a class="footnote-reference" href="#pyroname" id="id2">[1]</a></p>
</dd>
</dl>
</li>
<li><dl class="first docutils">
<dt>use object metadata tagging to look it up (yellow-pages style lookup).</dt>
<dd><p class="first">You can do this directly via the name server for maximum control, or use the <code class="docutils literal"><span class="pre">PYROMETA</span></code> protocol type.
See <a class="reference internal" href="nameserver.html#nameserver-pyrometa"><span class="std std-ref">The PYROMETA protocol type</span></a>. This means you can write:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">uri_string</span> <span class="o">=</span> <span class="s2">"PYROMETA:metatag1,metatag2"</span>
<span class="c1"># or Pyro4.URI("PYROMETA:metatag1,metatag2") for an URI object</span>
</pre></div>
</div>
<p class="last">You can use this URI everywhere you would normally use a normal uri.
Everytime Pyro encounters the <code class="docutils literal"><span class="pre">PYROMETA</span></code> uri it will use the name server automatically
to find a random object for you with the given metadata tags. <a class="footnote-reference" href="#pyroname" id="id3">[1]</a></p>
</dd>
</dl>
</li>
</ul>
<table class="docutils footnote" frame="void" id="pyroname" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label">[1]</td><td><em>(<a class="fn-backref" href="#id2">1</a>, <a class="fn-backref" href="#id3">2</a>)</em> this is not very efficient if it occurs often. Have a look at the <a class="reference internal" href="tipstricks.html"><span class="doc">Tips & Tricks</span></a>
chapter for some hints about this.</td></tr>
</tbody>
</table>
</div>
<div class="section" id="calling-methods">
<span id="index-3"></span><h2>Calling methods<a class="headerlink" href="#calling-methods" title="Permalink to this headline">¶</a></h2>
<p>Once you have the location of the Pyro object you want to talk to, you create a Proxy for it.
Normally you would perhaps create an instance of a class, and invoke methods on that object.
But with Pyro, your remote method calls on Pyro objects go trough a proxy.
The proxy can be treated as if it was the actual object, so you write normal python code
to call the remote methods and deal with the return values, or even exceptions:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1"># Continuing our imaginary music server example.</span>
<span class="c1"># Assume that uri contains the uri for the music server object.</span>
<span class="n">musicserver</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="k">try</span><span class="p">:</span>
<span class="n">musicserver</span><span class="o">.</span><span class="n">load_playlist</span><span class="p">(</span><span class="s2">"90s rock"</span><span class="p">)</span>
<span class="n">musicserver</span><span class="o">.</span><span class="n">play</span><span class="p">()</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Currently playing:"</span><span class="p">,</span> <span class="n">musicserver</span><span class="o">.</span><span class="n">current_song</span><span class="p">())</span>
<span class="k">except</span> <span class="n">MediaServerException</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Couldn't select playlist or start playing"</span><span class="p">)</span>
</pre></div>
</div>
<p>For normal usage, there’s not a single line of Pyro specific code once you have a proxy!</p>
<span class="target" id="index-4"></span></div>
<div class="section" id="accessing-remote-attributes">
<span id="index-5"></span><h2>Accessing remote attributes<a class="headerlink" href="#accessing-remote-attributes" title="Permalink to this headline">¶</a></h2>
<p>You can access exposed attributes of your remote objects directly via the proxy.
If you try to access an undefined or unexposed attribute, the proxy will raise an AttributeError stating the problem.
Note that direct remote attribute access only works if the metadata feature is enabled (<code class="docutils literal"><span class="pre">METADATA</span></code> config item, enabled by default).</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">Pyro4</span>
<span class="n">p</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="s2">"..."</span><span class="p">)</span>
<span class="n">velo</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">velocity</span> <span class="c1"># attribute access, no method call</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"velocity = "</span><span class="p">,</span> <span class="n">velo</span><span class="p">)</span>
</pre></div>
</div>
<p>See the <code class="file docutils literal"><span class="pre">attributes</span></code> example for more information.</p>
</div>
<div class="section" id="serialization">
<span id="object-serialization"></span><h2>Serialization<a class="headerlink" href="#serialization" title="Permalink to this headline">¶</a></h2>
<p>Pyro will serialize the objects that you pass to the remote methods, so they can be sent across
a network connection. Depending on the serializer that is being used, there will be some limitations
on what objects you can use.</p>
<ul class="simple">
<li><strong>serpent</strong>: serializes into Python literal expressions. Accepts quite a lot of different types.
Many will be serialized as dicts. You might need to explicitly translate literals back to specific types
on the receiving end if so desired, because most custom classes aren’t dealt with automatically.
Requires third party library module, but it will be installed automatically as a dependency of Pyro.
This serializer is the default choice.</li>
<li><strong>json</strong>: more restricted as serpent, less types supported. Part of the standard library. Not particularly fast,
so you might want to look for a faster 3rd party implementation (such as simplejson). Be sure to benchmark before switching!
Use the <cite>JSON_MODULE</cite> config item to tell Pyro to use the other module instead. Note that it has to support
the advanced parameters such as <cite>default</cite>, not all 3rd party implementations do that.</li>
<li><strong>marshal</strong>: a very limited but fast serializer. Can deal with a small range of builtin types only,
no custom classes can be serialized. Part of the standard library.</li>
<li><strong>msgpack</strong>: See <a class="reference external" href="https://pypi.python.org/pypi/msgpack-python">https://pypi.python.org/pypi/msgpack-python</a> Reasonably fast serializer (and a lot faster if you’re using the C module extension).
Can deal with many builtin types, but not all. Not enabled by default because it’s optional,
but it’s safe to add to the accepted serializers config item if you have it installed.</li>
<li><strong>pickle</strong>: the legacy serializer. Fast and supports almost all types. Part of the standard library.
Has security problems, so it’s better to avoid using it.</li>
<li><strong>cloudpickle</strong>: See <a class="reference external" href="https://pypi.python.org/pypi/cloudpickle">https://pypi.python.org/pypi/cloudpickle</a> It is similar to pickle serializer, but more capable. Extends python’s ‘pickle’ module
for serializing and de-serializing python objects to the majority of the built-in python types.
Has security problems though, just as pickle.</li>
<li><strong>dill</strong>: See <a class="reference external" href="https://pypi.python.org/pypi/dill">https://pypi.python.org/pypi/dill</a> It is similar to pickle serializer, but more capable. Extends python’s ‘pickle’ module
for serializing and de-serializing python objects to the majority of the built-in python types.
Has security problems though, just as pickle.</li>
</ul>
<p id="index-6">You select the serializer to be used by setting the <code class="docutils literal"><span class="pre">SERIALIZER</span></code> config item. (See the <a class="reference internal" href="config.html"><span class="doc">Configuring Pyro</span></a> chapter).
The valid choices are the names of the serializer from the list mentioned above.
If you’re using pickle or dill, and need to control the protocol version that is used,
you can do so with the <code class="docutils literal"><span class="pre">PICKLE_PROTOCOL_VERSION</span></code> or <code class="docutils literal"><span class="pre">DILL_PROTOCOL_VERSION</span></code> config items.
If you’re using cloudpickle, you can control the protocol version with <code class="docutils literal"><span class="pre">PICKLE_PROTOCOL_VERSION</span></code> as well.
By default Pyro will use the highest one available.</p>
<p>It is possible to override the serializer on a particular proxy. This allows you to connect to one server
using the default serpent serializer and use another proxy to connect to a different server using the json
serializer, for instance. Set the desired serializer name in <code class="docutils literal"><span class="pre">proxy._pyroSerializer</span></code> to override.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Since Pyro 4.20 the default serializer is “<code class="docutils literal"><span class="pre">serpent</span></code>”. Serpent is secure but cannot
serialize all types (by design). Some types are serialized into a different form such as
a string or a dict. Strings are serialized/deserialized into unicode at all times – be aware
of this if you’re using Python 2.x (strings in Python 3.x are always unicode already).</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The serializer(s) that a Pyro server/daemon accepts, is controlled by a different
config item (<code class="docutils literal"><span class="pre">SERIALIZERS_ACCEPTED</span></code>). This can be a set of one or more serializers.
By default it accepts the set of ‘safe’ serializers, so “<code class="docutils literal"><span class="pre">pickle</span></code>”, “<code class="docutils literal"><span class="pre">cloudpickle</span></code>”
and “<code class="docutils literal"><span class="pre">dill</span></code>” are excluded. If the server doesn’t accept the serializer that you configured
for your client, it will refuse the requests and respond with an exception that tells
you about the unsupported serializer choice. If it <em>does</em> accept your requests,
the server response will use the same serializer that was used for the request.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Because the name server is just a regular Pyro server as well, you will have to tell
it to allow the pickle, cloudpickle or dill serializers if your client code uses them.
See <a class="reference internal" href="nameserver.html#nameserver-pickle"><span class="std std-ref">Using the name server with pickle, cloudpickle or dill serializers</span></a>.</p>
</div>
<div class="section" id="changing-the-way-your-custom-classes-are-de-serialized">
<span id="customizing-serialization"></span><span id="index-7"></span><h3>Changing the way your custom classes are (de)serialized<a class="headerlink" href="#changing-the-way-your-custom-classes-are-de-serialized" title="Permalink to this headline">¶</a></h3>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The information in this paragraph is not relevant when using the pickle, cloudpickle or dill serialization protocols,
they have their own ways of serializing custom classes.</p>
</div>
<p>By default, custom classes are serialized into a dict.
They are not deserialized back into instances of your custom class. This avoids possible security issues.
An exception to this however are certain classes in the Pyro4 package itself (such as the URI and Proxy classes).
They <em>are</em> deserialized back into objects of that certain class, because they are critical for Pyro to function correctly.</p>
<p>There are a few hooks however that allow you to extend this default behaviour and register certain custom
converter functions. These allow you to change the way your custom classes are treated, and allow you
to actually get instances of your custom class back from the deserialization if you so desire.</p>
<dl class="docutils">
<dt>The hooks are provided via several classmethods:</dt>
<dd><a class="reference internal" href="api/util.html#Pyro4.util.SerializerBase.register_class_to_dict" title="Pyro4.util.SerializerBase.register_class_to_dict"><code class="xref py py-meth docutils literal"><span class="pre">Pyro4.util.SerializerBase.register_class_to_dict()</span></code></a> and <a class="reference internal" href="api/util.html#Pyro4.util.SerializerBase.register_dict_to_class" title="Pyro4.util.SerializerBase.register_dict_to_class"><code class="xref py py-meth docutils literal"><span class="pre">Pyro4.util.SerializerBase.register_dict_to_class()</span></code></a></dd>
<dt>and their unregister-counterparts:</dt>
<dd><a class="reference internal" href="api/util.html#Pyro4.util.SerializerBase.unregister_class_to_dict" title="Pyro4.util.SerializerBase.unregister_class_to_dict"><code class="xref py py-meth docutils literal"><span class="pre">Pyro4.util.SerializerBase.unregister_class_to_dict()</span></code></a> and <a class="reference internal" href="api/util.html#Pyro4.util.SerializerBase.unregister_dict_to_class" title="Pyro4.util.SerializerBase.unregister_dict_to_class"><code class="xref py py-meth docutils literal"><span class="pre">Pyro4.util.SerializerBase.unregister_dict_to_class()</span></code></a></dd>
</dl>
<p>Click on the method link to see its apidoc, or have a look at the <code class="file docutils literal"><span class="pre">ser_custom</span></code> example and the <code class="file docutils literal"><span class="pre">test_serialize</span></code> unit tests for more information.
It is recommended to avoid using these hooks if possible, there’s a security risk
to create arbitrary objects from serialized data that is received from untrusted sources.</p>
</div>
<div class="section" id="upgrading-older-code-that-relies-on-pickle">
<h3>Upgrading older code that relies on pickle<a class="headerlink" href="#upgrading-older-code-that-relies-on-pickle" title="Permalink to this headline">¶</a></h3>
<p>What do you have to do with code that relies on pickle, and worked fine in older Pyro versions, but now crashes?</p>
<p>You have three options:</p>
<ol class="arabic simple">
<li>Redesign remote interfaces</li>
<li>Configure Pyro to eable the use of pickle again</li>
<li>Stick to Pyro 4.18 (less preferable)</li>
</ol>
<p>You can redesign the remote interface to only include types that can be serialized (python’s built-in types and
exception classes, and a few Pyro specific classes such as URIs). That way you benefit from the new security that
the alternative serializers provide. If you can’t do this, you have to tell Pyro to enable pickle again.
This has been made an explicit step because of the security implications of using pickle. Here’s how to do this:</p>
<dl class="docutils">
<dt>Client code configuration</dt>
<dd>Tell Pyro to use pickle as serializer for outgoing communication, by setting the <code class="docutils literal"><span class="pre">SERIALIZER</span></code>
config item to <code class="docutils literal"><span class="pre">pickle</span></code>. For instance, in your code: <code class="code docutils literal"><span class="pre">Pyro4.config.SERIALIZER</span> <span class="pre">=</span> <span class="pre">'pickle'</span></code>
or set the appropriate environment variable.</dd>
<dt>Server code configuration</dt>
<dd>Tell Pyro to accept pickle as incoming serialization format, by including <code class="docutils literal"><span class="pre">pickle</span></code> in
the <code class="docutils literal"><span class="pre">SERIALIZERS_ACCEPTED</span></code> config item list. For instance, in your code:
<code class="code docutils literal"><span class="pre">Pyro4.config.SERIALIZERS_ACCEPTED.add('pickle')</span></code>. Or set the appropriate
environment variable, for instance: <code class="code docutils literal"><span class="pre">export</span> <span class="pre">PYRO_SERIALIZERS_ACCEPTED=serpent,json,marshal,pickle</span></code>.
If your server also uses Pyro to call other servers, you may also need to configure
it as mentioned above at ‘client code’. This is because the incoming and outgoing serializer formats
are configured independently.</dd>
</dl>
<span class="target" id="index-8"></span></div>
</div>
<div class="section" id="proxies-connections-threads-and-cleaning-up">
<span id="client-cleanup"></span><span id="index-9"></span><h2>Proxies, connections, threads and cleaning up<a class="headerlink" href="#proxies-connections-threads-and-cleaning-up" title="Permalink to this headline">¶</a></h2>
<p>Here are some rules:</p>
<ul>
<li><p class="first">Every single Proxy object will have its own socket connection to the daemon.</p>
</li>
<li><p class="first">You can share Proxy objects among threads, it will re-use the same socket connection.</p>
</li>
<li><p class="first">Usually every connection in the daemon has its own processing thread there, but for more details see the <a class="reference internal" href="servercode.html"><span class="doc">Servers: hosting Pyro objects</span></a> chapter.</p>
</li>
<li><p class="first">The connection will remain active for the lifetime of the proxy object. Hence, consider cleaning up a proxy object explicitly
if you know you won’t be using it again in a while. That will free up resources and socket connections.
You can do this in two ways:</p>
<ol class="arabic">
<li><p class="first">calling <code class="docutils literal"><span class="pre">_pyroRelease()</span></code> on the proxy.</p>
</li>
<li><p class="first">using the proxy as a context manager in a <code class="docutils literal"><span class="pre">with</span></code> statement. <em>This is the preffered way of creating and using Pyro proxies.</em>
This ensures that when you’re done with it, or an error occurs (inside the with-block),
the connection is released:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="n">Pyro4</span><span class="o">.</span><span class="n">Proxy</span><span class="p">(</span><span class="s2">"....."</span><span class="p">)</span> <span class="k">as</span> <span class="n">obj</span><span class="p">:</span>
<span class="n">obj</span><span class="o">.</span><span class="n">method</span><span class="p">()</span>
</pre></div>
</div>
</li>
</ol>
<p><em>Note:</em> you can still use the proxy object when it is disconnected: Pyro will reconnect it as soon as it’s needed again.</p>
</li>
<li><p class="first">At proxy creation, no actual connection is made. The proxy is only actually connected at first use, or when you manually
connect it using the <code class="docutils literal"><span class="pre">_pyroReconnect()</span></code> or <code class="docutils literal"><span class="pre">_pyroBind()</span></code> methods.</p>
</li>
</ul>
</div>
<div class="section" id="oneway-calls">
<span id="oneway-calls-client"></span><span id="index-10"></span><h2>Oneway calls<a class="headerlink" href="#oneway-calls" title="Permalink to this headline">¶</a></h2>
<p>Normal method calls always block until the response is returned. This can be any normal return value, <code class="docutils literal"><span class="pre">None</span></code>,
or an error in the form of a raised exception. The client code execution is suspended until the method call
has finished and produced its result.</p>
<p>Some methods never return any response or you are simply not interested in it (including errors and
exceptions!), or you don’t want to wait until the result is available but rather continue immediately.
You can tell Pyro that calls to these methods should be done as <em>one-way calls</em>.
For calls to such methods, Pyro will not wait for a response from the remote object.
The return value of these calls is always <code class="docutils literal"><span class="pre">None</span></code>, which is returned <em>immediately</em> after submitting the method
invocation to the server. The server will process the call while your client continues execution.
The client can’t tell if the method call was successful, because no return value, no errors and no exceptions will be returned!
If you want to find out later what - if anything - happened, you have to call another (non-oneway) method that does return a value.</p>
<p>Note that this is different from <a class="reference internal" href="#async-calls"><span class="std std-ref">Asynchronous (‘future’) remote calls & call chains</span></a>: they are also executed while your client code
continues with its work, but they <em>do</em> return a value (but at a later moment in time). Oneway calls
are more efficient because they immediately produce <code class="docutils literal"><span class="pre">None</span></code> as result and that’s it.</p>
<p id="index-11"><strong>How to make methods one-way:</strong>
You mark the methods of your class <em>in the server</em> as one-way by using a special <em>decorator</em>.
See <a class="reference internal" href="servercode.html#decorating-pyro-class"><span class="std std-ref">Creating a Pyro class and exposing its methods and properties</span></a> for details on how to do this.
See the <code class="file docutils literal"><span class="pre">oneway</span></code> example for some code that demonstrates the use of oneway methods.</p>
</div>
<div class="section" id="batched-calls">
<span id="index-12"></span><span id="id4"></span><h2>Batched calls<a class="headerlink" href="#batched-calls" title="Permalink to this headline">¶</a></h2>
<p>Doing many small remote method calls in sequence has a fair amount of latency and overhead.
Pyro provides a means to gather all these small calls and submit it as a single ‘batched call’.
When the server processed them all, you get back all results at once.
Depending on the size of the arguments, the network speed, and the amount of calls,
doing a batched call can be <em>much</em> faster than invoking every call by itself.
Note that this feature is only available for calls on the same proxy object.</p>
<p>How it works:</p>
<ol class="arabic simple">
<li>You create a batch proxy object for the proxy object.</li>
<li>Call all the methods you would normally call on the regular proxy, but use the batch proxy object instead.</li>
<li>Call the batch proxy object itself to obtain the generator with the results.</li>
</ol>
<p>You create a batch proxy using this: <code class="docutils literal"><span class="pre">batch</span> <span class="pre">=</span> <span class="pre">Pyro4.batch(proxy)</span></code> or this (equivalent): <code class="docutils literal"><span class="pre">batch</span> <span class="pre">=</span> <span class="pre">proxy._pyroBatch()</span></code>.
The signature of the batch proxy call is as follows:</p>
<dl class="method">
<dt id="batchproxy.__call__">
<code class="descclassname">batchproxy.</code><code class="descname">__call__</code><span class="sig-paren">(</span><span class="optional">[</span><em>oneway=False</em>, <em>async=False</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#batchproxy.__call__" title="Permalink to this definition">¶</a></dt>
<dd><p>Invoke the batch and when done, returns a generator that produces the results of every call, in order.
If <code class="docutils literal"><span class="pre">oneway==True</span></code>, perform the whole batch as one-way calls, and return <code class="docutils literal"><span class="pre">None</span></code> immediately.
If <code class="docutils literal"><span class="pre">async==True</span></code>, perform the batch asynchronously, and return an asynchronous call result object immediately.</p>
</dd></dl>
<p><strong>Simple example</strong>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">batch</span> <span class="o">=</span> <span class="n">Pyro4</span><span class="o">.</span><span class="n">batch</span><span class="p">(</span><span class="n">proxy</span><span class="p">)</span>
<span class="n">batch</span><span class="o">.</span><span class="n">method1</span><span class="p">()</span>
<span class="n">batch</span><span class="o">.</span><span class="n">method2</span><span class="p">()</span>
<span class="c1"># more calls ...</span>
<span class="n">batch</span><span class="o">.</span><span class="n">methodN</span><span class="p">()</span>
<span class="n">results</span> <span class="o">=</span> <span class="n">batch</span><span class="p">()</span> <span class="c1"># execute the batch</span>
<span class="k">for</span> <span class="n">result</span> <span class="ow">in</span> <span class="n">results</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="n">result</span><span class="p">)</span> <span class="c1"># process result in order of calls...</span>
</pre></div>
</div>
<p><strong>Oneway batch</strong>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">results</span> <span class="o">=</span> <span class="n">batch</span><span class="p">(</span><span class="n">oneway</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="c1"># results==None</span>
</pre></div>
</div>
<p><strong>Asynchronous batch</strong></p>
<p>The result value of an asynchronous batch call is a special object. See <a class="reference internal" href="#async-calls"><span class="std std-ref">Asynchronous (‘future’) remote calls & call chains</span></a> for more details about it.
This is some simple code doing an asynchronous batch:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">results</span> <span class="o">=</span> <span class="n">batch</span><span class="p">(</span><span class="k">async</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="c1"># do some stuff... until you're ready and require the results of the async batch:</span>
<span class="k">for</span> <span class="n">result</span> <span class="ow">in</span> <span class="n">results</span><span class="o">.</span><span class="n">value</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="n">result</span><span class="p">)</span> <span class="c1"># process the results</span>
</pre></div>
</div>
<p>See the <code class="file docutils literal"><span class="pre">batchedcalls</span></code> example for more details.</p>
</div>
<div class="section" id="remote-iterators-generators">
<span id="index-13"></span><h2>Remote iterators/generators<a class="headerlink" href="#remote-iterators-generators" title="Permalink to this headline">¶</a></h2>
<p>Since Pyro 4.49 it is possible to simply iterate over a remote iterator or generator function as if it
was a perfectly normal Python iterable. Pyro will fetch the items one by one from the server that is
running the remote iterator until all elements have been consumed or the client disconnects.</p>
<div class="sidebar">
<p class="first sidebar-title"><em>Filter on the server</em></p>
<p>If you plan to filter the items that are returned from the iterator,
it is strongly suggested to do that on the server and not in your client.
Because otherwise it is possible that you first have
to serialize and transfer all possible items from the server only to select
a few out of them, which is very inefficient.</p>
<p><em>Beware of many small items</em></p>
<p class="last">Pyro has to do a remote call to get every next item from the iterable.
If your iterator produces lots of small individual items, this can be quite
inefficient (many small network calls). Either chunk them up a bit or
use larger individual items.</p>
</div>
<p>So you can write in your client:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">proxy</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="s2">"..."</span><span class="p">)</span>
<span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">proxy</span><span class="o">.</span><span class="n">things</span><span class="p">():</span>
<span class="nb">print</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
</pre></div>
</div>
<p>The implementation of the <code class="docutils literal"><span class="pre">things</span></code> method can return a normal list but can
also return an iterator or even be a generator function itself. This has the usual benefits of “lazy” generators:
no need to create the full collection upfront which can take a lot of memory, possibility
of infinite sequences, and spreading computation load more evenly.</p>
<p>By default the remote item streaming is enabled in the server and there is no time limit set
for how long iterators and generators can be ‘alive’ in the server. You can configure this however
if you want to restrict resource usage or disable this feature altogether, via the
<code class="docutils literal"><span class="pre">ITER_STREAMING</span></code> and <code class="docutils literal"><span class="pre">ITER_STREAM_LIFETIME</span></code> config items.</p>
<p>Lingering when disconnected: the <code class="docutils literal"><span class="pre">ITER_STREAM_LINGER</span></code> config item controls the number of seconds
a remote generator is kept alive when a disconnect happens. It defaults to 30 seconds. This allows
you to reconnect the proxy and continue using the remote generator as if nothing happened
(see <a class="reference internal" href="api/core.html#Pyro4.core.Proxy._pyroReconnect" title="Pyro4.core.Proxy._pyroReconnect"><code class="xref py py-meth docutils literal"><span class="pre">Pyro4.core.Proxy._pyroReconnect()</span></code></a> or even <a class="reference internal" href="#reconnecting"><span class="std std-ref">Automatic reconnecting</span></a>). If you reconnect the
proxy and continue iterating again <em>after</em> the lingering timeout period expired, an exception is thrown
because the remote generator has been discarded in the meantime.
Lingering can be disabled completely by setting the value to 0, then all remote generators from a proxy will
immediately be discarded in the server if the proxy gets disconnected or closed.</p>
<p>Notice that you can also use this in your Java or .NET/C# programs that connect to Python via
Pyrolite! Version 4.14 or newer of that library supports Pyro item streaming. It returns normal
Java and .NET iterables to your code that you can loop over normally with foreach or other things.</p>
<p>There are several examples that use the remote iterator feature.
Have a look at the <code class="file docutils literal"><span class="pre">stockquotes</span></code> tutorial example, or the <code class="file docutils literal"><span class="pre">filetransfer</span></code> example.</p>
</div>
<div class="section" id="asynchronous-future-remote-calls-call-chains">
<span id="async-calls"></span><span id="index-14"></span><h2>Asynchronous (‘future’) remote calls & call chains<a class="headerlink" href="#asynchronous-future-remote-calls-call-chains" title="Permalink to this headline">¶</a></h2>
<p>You can execute a remote method call and tell Pyro: “hey, I don’t need the results right now.
Go ahead and compute them, I’ll come back later once I need them”.
The call will be processed in the background and you can collect the results at a later time.
If the results are not yet available (because the call is <em>still</em> being processed) your code blocks
but only at the line you are actually retrieving the results. If they have become available in the
meantime, the code doesn’t block at all and can process the results immediately.
It is possible to define one or more callables (the “call chain”) that should be invoked
automatically by Pyro as soon as the result value becomes available.</p>
<p>You set a proxy in async mode using this: <code class="docutils literal"><span class="pre">Pyro4.async(proxy)</span></code> or (equivalent): <code class="docutils literal"><span class="pre">proxy._pyroAsync()</span></code>.
Every remote method call you make on the async proxy, returns a
<a class="reference internal" href="api/futures.html#Pyro4.futures.FutureResult" title="Pyro4.futures.FutureResult"><code class="xref py py-class docutils literal"><span class="pre">Pyro4.futures.FutureResult</span></code></a> object immediately.
This object means ‘the result of this will be available at some moment in the future’ and has the following interface:</p>
<dl class="attribute">
<dt id="value">
<code class="descname">value</code><a class="headerlink" href="#value" title="Permalink to this definition">¶</a></dt>
<dd><p>This property contains the result value from the call.
If you read this and the value is not yet available, execution is halted until the value becomes available.
If it is already available you can read it as usual.</p>
</dd></dl>
<dl class="attribute">
<dt id="ready">
<code class="descname">ready</code><a class="headerlink" href="#ready" title="Permalink to this definition">¶</a></dt>
<dd><p>This property contains the readiness of the result value (<code class="docutils literal"><span class="pre">True</span></code> meaning that the value is available).</p>
</dd></dl>
<dl class="method">
<dt id="wait">
<code class="descname">wait</code><span class="sig-paren">(</span><span class="optional">[</span><em>timeout=None</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#wait" title="Permalink to this definition">¶</a></dt>
<dd><p>Waits for the result value to become available, with optional wait timeout (in seconds). Default is None,
meaning infinite timeout. If the timeout expires before the result value is available, the call
will return <code class="docutils literal"><span class="pre">False</span></code>. If the value has become available, it will return <code class="docutils literal"><span class="pre">True</span></code>.</p>
</dd></dl>
<dl class="method">
<dt id="then">
<code class="descname">then</code><span class="sig-paren">(</span><em>callable</em><span class="optional">[</span>, <em>*args</em>, <em>**kwargs</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#then" title="Permalink to this definition">¶</a></dt>
<dd><p>Add a callable to the call chain, to be invoked when the results become available.
The result of the current call will be used as the first argument for the next call.
Optional extra arguments can be provided via <code class="docutils literal"><span class="pre">args</span></code> and <code class="docutils literal"><span class="pre">kwargs</span></code>.</p>
</dd></dl>
<dl class="method">
<dt id="iferror">
<code class="descname">iferror</code><span class="sig-paren">(</span><em>errorhandler</em><span class="sig-paren">)</span><a class="headerlink" href="#iferror" title="Permalink to this definition">¶</a></dt>
<dd><p>Specify the exception handler to be invoked (with the exception object as only
argument) when asking for the result raises an exception.
If no exception handler is set, any exception result will be silently ignored (unless
you explicitly ask for the value). Returns self so you can easily chain other calls.</p>
</dd></dl>
<p>A simple piece of code showing an asynchronous method call:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">proxy</span><span class="o">.</span><span class="n">_pyroAsync</span><span class="p">()</span>
<span class="n">asyncresult</span> <span class="o">=</span> <span class="n">proxy</span><span class="o">.</span><span class="n">remotemethod</span><span class="p">()</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"value available?"</span><span class="p">,</span> <span class="n">asyncresult</span><span class="o">.</span><span class="n">ready</span><span class="p">)</span>
<span class="c1"># ...do some other stuff...</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"resultvalue="</span><span class="p">,</span> <span class="n">asyncresult</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><a class="reference internal" href="#batched-calls"><span class="std std-ref">Batched calls</span></a> can also be executed asynchronously.
Asynchronous calls are implemented using a background thread that waits for the results.
Callables from the call chain are invoked sequentially in this background thread.</p>
</div>
<p>See the <code class="file docutils literal"><span class="pre">async</span></code> example for more details and example code for call chains.</p>
<div class="section" id="async-calls-for-normal-callables-not-only-for-pyro-proxies">
<h3>Async calls for normal callables (not only for Pyro proxies)<a class="headerlink" href="#async-calls-for-normal-callables-not-only-for-pyro-proxies" title="Permalink to this headline">¶</a></h3>
<p>The async proxy discussed above is only available when you are dealing with Pyro proxies.
It provides a convenient syntax to call the methods on the proxy asynchronously.
For normal Python code it is sometimes useful to have a similar mechanism as well.
Pyro provides this too, see <a class="reference internal" href="tipstricks.html#future-functions"><span class="std std-ref">Asynchronous (‘future’) normal function calls</span></a> for more information.</p>
</div>
</div>
<div class="section" id="pyro-callbacks">
<span id="index-15"></span><h2>Pyro Callbacks<a class="headerlink" href="#pyro-callbacks" title="Permalink to this headline">¶</a></h2>
<p>Usually there is a nice separation between a server and a client.
But with some Pyro programs it is not that simple.
It isn’t weird for a Pyro object in a server somewhere to invoke a method call
on another Pyro object, that could even be running in the client program doing the initial call.
In this case the client program is a server itself as well.</p>
<p>These kinds of ‘reverse’ calls are labeled <em>callbacks</em>. You have to do a bit of
work to make them possible, because normally, a client program is not running the required
code to also act as a Pyro server to accept incoming callback calls.</p>
<p>In fact, you have to start a Pyro daemon and register the callback Pyro objects in it,
just as if you were writing a server program.
Keep in mind though that you probably have to run the daemon’s request loop in its own
background thread. Or make heavy use of oneway method calls.
If you don’t, your client program won’t be able to process the callback requests because
it is by itself still waiting for results from the server.</p>
<p id="index-16"><strong>Exceptions in callback objects:</strong>
If your callback object raises an exception, Pyro will return that to the server doing the
callback. Depending on what the server does with it, you might never see the actual exception,
let alone the stack trace. This is why Pyro provides a decorator that you can use
on the methods in your callback object in the client program: <code class="docutils literal"><span class="pre">@Pyro4.callback</span></code>.
This way, an exception in that method is not only returned to the caller, but also
logged locally in your client program, so you can see it happen including the
stack trace (if you have logging enabled):</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">Pyro4</span>
<span class="k">class</span> <span class="nc">Callback</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="nd">@Pyro4</span><span class="o">.</span><span class="n">expose</span>
<span class="nd">@Pyro4</span><span class="o">.</span><span class="n">callback</span>
<span class="k">def</span> <span class="nf">call</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"callback received from server!"</span><span class="p">)</span>
<span class="k">return</span> <span class="mi">1</span><span class="o">//</span><span class="mi">0</span> <span class="c1"># crash!</span>
</pre></div>
</div>
<p>Also notice that the callback method (or the whole class) has to be decorated
with <code class="docutils literal"><span class="pre">@Pyro4.expose</span></code> as well to allow it to be called remotely at all.
See the <code class="file docutils literal"><span class="pre">callback</span></code> example for more details and code.</p>
</div>
<div class="section" id="miscellaneous-features">
<span id="index-17"></span><h2>Miscellaneous features<a class="headerlink" href="#miscellaneous-features" title="Permalink to this headline">¶</a></h2>
<p>Pyro provides a few miscellaneous features when dealing with remote method calls.
They are described in this section.</p>
<div class="section" id="error-handling">
<span id="index-18"></span><h3>Error handling<a class="headerlink" href="#error-handling" title="Permalink to this headline">¶</a></h3>
<p>You can just do exception handling as you would do when writing normal Python code.
However, Pyro provides a few extra features when dealing with errors that occurred in
remote objects. This subject is explained in detail its own chapter: <a class="reference internal" href="errors.html"><span class="doc">Exceptions and remote tracebacks</span></a>.</p>
<p>See the <code class="file docutils literal"><span class="pre">exceptions</span></code> example for more details.</p>
</div>
<div class="section" id="timeouts">
<span id="index-19"></span><h3>Timeouts<a class="headerlink" href="#timeouts" title="Permalink to this headline">¶</a></h3>
<p>Because calls on Pyro objects go over the network, you might encounter network related problems that you
don’t have when using normal objects. One possible problems is some sort of network hiccup
that makes your call unresponsive because the data never arrived at the server or the response never
arrived back to the caller.</p>
<p>By default, Pyro waits an indefinite amount of time for the call to return. You can choose to
configure a <em>timeout</em> however. This can be done globally (for all Pyro network related operations)
by setting the timeout config item:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">Pyro4</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">COMMTIMEOUT</span> <span class="o">=</span> <span class="mf">1.5</span> <span class="c1"># 1.5 seconds</span>
</pre></div>
</div>
<p>You can also do this on a per-proxy basis by setting the timeout property on the proxy:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">proxy</span><span class="o">.</span><span class="n">_pyroTimeout</span> <span class="o">=</span> <span class="mf">1.5</span> <span class="c1"># 1.5 seconds</span>
</pre></div>
</div>
<p>There is also a server setting related to oneway calls, that says if oneway method
calls should be executed in a separate thread or not. If this is set to <code class="docutils literal"><span class="pre">False</span></code>,
they will execute in the same thread as the other method calls. This means that if the
oneway call is taking a long time to complete, the other method calls from the client may
actually stall, because they’re waiting on the server to complete the oneway call that
came before them. To avoid this problem you can set this config item to True (which is the default).
This runs the oneway call in its own thread (regardless of the server type that is used)
and other calls can be processed immediately:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">Pyro4</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">ONEWAY_THREADED</span> <span class="o">=</span> <span class="kc">True</span> <span class="c1"># this is the default</span>
</pre></div>
</div>
<p>See the <code class="file docutils literal"><span class="pre">timeout</span></code> example for more details.</p>
<p>Also, there is a automatic retry mechanism for timeout or connection closed (by server side),
in order to use this automatically retry:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">Pyro4</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">MAX_RETRIES</span> <span class="o">=</span> <span class="mi">3</span> <span class="c1"># attempt to retry 3 times before raise the exception</span>
</pre></div>
</div>
<p>You can also do this on a pre-proxy basis by setting the max retries property on the proxy:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">proxy</span><span class="o">.</span><span class="n">_pyroMaxRetries</span> <span class="o">=</span> <span class="mi">3</span> <span class="c1"># attempt to retry 3 times before raise the exception</span>
</pre></div>
</div>
<p>Be careful to use when remote functions have a side effect (e.g.: calling twice results in error)!
See the <code class="file docutils literal"><span class="pre">autoretry</span></code> example for more details.</p>
</div>
<div class="section" id="automatic-reconnecting">
<span id="reconnecting"></span><span id="index-20"></span><h3>Automatic reconnecting<a class="headerlink" href="#automatic-reconnecting" title="Permalink to this headline">¶</a></h3>
<p>If your client program becomes disconnected to the server (because the server crashed for instance),
Pyro will raise a <a class="reference internal" href="api/errors.html#Pyro4.errors.ConnectionClosedError" title="Pyro4.errors.ConnectionClosedError"><code class="xref py py-exc docutils literal"><span class="pre">Pyro4.errors.ConnectionClosedError</span></code></a>.
You can use the automatic retry mechanism to handle this exception, see the <code class="file docutils literal"><span class="pre">autoretry</span></code> example for more details.
Alternatively, it is also possible to catch this and tell Pyro to attempt to reconnect to the server by calling
<code class="docutils literal"><span class="pre">_pyroReconnect()</span></code> on the proxy (it takes an optional argument: the number of attempts
to reconnect to the daemon. By default this is almost infinite). Once successful, you can resume operations
on the proxy:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span>
<span class="n">proxy</span><span class="o">.</span><span class="n">method</span><span class="p">()</span>
<span class="k">except</span> <span class="n">Pyro4</span><span class="o">.</span><span class="n">errors</span><span class="o">.</span><span class="n">ConnectionClosedError</span><span class="p">:</span>
<span class="c1"># connection lost, try reconnecting</span>
<span class="n">obj</span><span class="o">.</span><span class="n">_pyroReconnect</span><span class="p">()</span>
</pre></div>
</div>
<p>This will only work if you take a few precautions in the server. Most importantly, if it crashed and comes
up again, it needs to publish its Pyro objects with the exact same URI as before (object id, hostname, daemon
port number).</p>
<p>See the <code class="file docutils literal"><span class="pre">autoreconnect</span></code> example for more details and some suggestions on how to do this.</p>
<p>The <code class="docutils literal"><span class="pre">_pyroReconnect()</span></code> method can also be used to force a newly created proxy to connect immediately,
rather than on first use.</p>
</div>
<div class="section" id="proxy-sharing">
<span id="index-21"></span><h3>Proxy sharing<a class="headerlink" href="#proxy-sharing" title="Permalink to this headline">¶</a></h3>
<p>Due to internal locking you can freely share proxies among threads.
The lock makes sure that only a single thread is actually using the proxy’s
communication channel at all times.
This can be convenient <em>but</em> it may not be the best way to approach things. The lock essentially
prevents parallelism. If you want calls to go in parallel, give each thread its own proxy.</p>
<p>Here are a couple of suggestions on how to make copies of a proxy:</p>
<ol class="arabic simple">
<li>use the <code class="xref py py-mod docutils literal"><span class="pre">copy</span></code> module, <code class="docutils literal"><span class="pre">proxy2</span> <span class="pre">=</span> <span class="pre">copy.copy(proxy)</span></code></li>
<li>create a new proxy from the uri of the old one: <code class="docutils literal"><span class="pre">proxy2</span> <span class="pre">=</span> <span class="pre">Pyro4.Proxy(proxy._pyroUri)</span></code></li>
<li>simply create a proxy in the thread itself (pass the uri to the thread instead of a proxy)</li>
</ol>
<p>See the <code class="file docutils literal"><span class="pre">proxysharing</span></code> example for more details.</p>
</div>
<div class="section" id="metadata-from-the-daemon">
<span id="metadata"></span><span id="index-22"></span><h3>Metadata from the daemon<a class="headerlink" href="#metadata-from-the-daemon" title="Permalink to this headline">¶</a></h3>
<p>A proxy contains some meta-data about the object it connects to.
It obtains the data via the (public) <a class="reference internal" href="api/core.html#Pyro4.core.DaemonObject.get_metadata" title="Pyro4.core.DaemonObject.get_metadata"><code class="xref py py-meth docutils literal"><span class="pre">Pyro4.core.DaemonObject.get_metadata()</span></code></a> method on the daemon
that it connects to. This method returns the following information about the object (or rather, its class):
what methods and attributes are defined, and which of the methods are to be called as one-way.
This information is used to properly execute one-way calls, and to do client-side validation of calls on the proxy
(for instance to see if a method or attribute is actually available, without having to do a round-trip to the server).
Also this enables a properly working <code class="docutils literal"><span class="pre">hasattr</span></code> on the proxy, and efficient and specific error messages
if you try to access a method or attribute that is not defined or not exposed on the Pyro object.
Lastly the direct access to attributes on the remote object is also made possible, because the proxy knows about what
attributes are available.</p>
<p>For backward compatibility with old Pyro4 versions (4.26 and older) you can disable this mechanism by setting the
<code class="docutils literal"><span class="pre">METADATA</span></code> config item to <code class="docutils literal"><span class="pre">False</span></code> (it’s <code class="docutils literal"><span class="pre">True</span></code> by default).
You can tell if you need to do this if you’re getting errors in your proxy saying that ‘DaemonObject’ has no attribute ‘get_metadata’.
Either upgrade the Pyro version of the server, or set the <code class="docutils literal"><span class="pre">METADATA</span></code> config item to False in your client code.</p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<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="#">Clients: Calling remote objects</a><ul>
<li><a class="reference internal" href="#object-discovery">Object discovery</a></li>
<li><a class="reference internal" href="#calling-methods">Calling methods</a></li>
<li><a class="reference internal" href="#accessing-remote-attributes">Accessing remote attributes</a></li>
<li><a class="reference internal" href="#serialization">Serialization</a><ul>
<li><a class="reference internal" href="#changing-the-way-your-custom-classes-are-de-serialized">Changing the way your custom classes are (de)serialized</a></li>
<li><a class="reference internal" href="#upgrading-older-code-that-relies-on-pickle">Upgrading older code that relies on pickle</a></li>
</ul>
</li>
<li><a class="reference internal" href="#proxies-connections-threads-and-cleaning-up">Proxies, connections, threads and cleaning up</a></li>
<li><a class="reference internal" href="#oneway-calls">Oneway calls</a></li>
<li><a class="reference internal" href="#batched-calls">Batched calls</a></li>
<li><a class="reference internal" href="#remote-iterators-generators">Remote iterators/generators</a></li>
<li><a class="reference internal" href="#asynchronous-future-remote-calls-call-chains">Asynchronous (‘future’) remote calls & call chains</a><ul>
<li><a class="reference internal" href="#async-calls-for-normal-callables-not-only-for-pyro-proxies">Async calls for normal callables (not only for Pyro proxies)</a></li>
</ul>
</li>
<li><a class="reference internal" href="#pyro-callbacks">Pyro Callbacks</a></li>
<li><a class="reference internal" href="#miscellaneous-features">Miscellaneous features</a><ul>
<li><a class="reference internal" href="#error-handling">Error handling</a></li>
<li><a class="reference internal" href="#timeouts">Timeouts</a></li>
<li><a class="reference internal" href="#automatic-reconnecting">Automatic reconnecting</a></li>
<li><a class="reference internal" href="#proxy-sharing">Proxy sharing</a></li>
<li><a class="reference internal" href="#metadata-from-the-daemon">Metadata from the daemon</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="commandline.html"
title="previous chapter">Command line tools</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="servercode.html"
title="next chapter">Servers: hosting Pyro objects</a></p>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="servercode.html" title="Servers: hosting Pyro objects"
>next</a> |</li>
<li class="right" >
<a href="commandline.html" title="Command line tools"
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">Pyro 4.63 documentation</a> »</li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright Irmen de Jong.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.6.5.
</div>
</body>
</html>
|