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