Proxy

class compas.rpc.Proxy(package=None, python=None, url='http://127.0.0.1', port=1753, service=None, max_conn_attempts=100, autoreload=True, capture_output=True)[source]

Bases: object

Create a proxy object as intermediary between client code and remote functionality.

This class is a context manager, so when used in a with statement, it ensures the remote proxy server is stopped and disposed correctly.

However, if the proxy server is left open, it can be re-used for a follow-up connection, saving start up time.

Parameters
  • package (str, optional) – The base package for the requested functionality. Default is None, in which case a full path to function calls should be provided.

  • python (str, optional) – The python executable that should be used to execute the code.

  • url (str, optional) – The server address.

  • port (int, optional) – The port number on the remote server.

  • service (str, optional) – Package name to start server. Default is 'compas.rpc.services.default'.

  • max_conn_attempts (int, optional) – Amount of attempts to connect to RPC server before time out.

  • autoreload (bool, optional) – If True, automatically reload the proxied package if changes are detected. This is particularly useful during development. The server will monitor changes to the files that were loaded as a result of accessing the specified package and if any change is detected, it will unload the module, so that the next invocation uses a fresh version.

  • capture_output (bool, optional) – If True, capture the stdout/stderr output of the remote process. In general, capture_output should be True when using a pythonw as executable (default).

Attributes
  • address (str, read-only) – Address of the server as a combination of url and port.

  • profile (str) – A profile of the code executed by the server.

  • package (str) – Fully qualified name of the package or module from where functions should be imported on the server side.

  • service (str) – Fully qualified package name required for starting the server/service.

  • python (str) – The type of Python executable that should be used to execute the code.

Notes

If the server is your localhost, which will often be the case, it is better to specify the address explicitly ('http://127.0.0.1') because resolving localhost takes a surprisingly significant amount of time.

The service will make the correct (version of the requested) functionality available even if that functionality is part of a virtual environment. This is because it will use the specific python interpreter for which the functionality is installed to start the server.

If possible, the proxy will try to reconnect to an already existing service

Examples

Minimal example showing connection to the proxy server, and ensuring the server is disposed after using it:

>>> from compas.rpc import Proxy                        
>>> with Proxy('compas.numerical') as numerical:        
...     pass                                            
...                                                     
Starting a new proxy server...                          
New proxy server started.                               
Stopping the server proxy.                              

Methods

restart_server

Restart the server.

start_server

Start the remote server.

stop_server

Stop the remote server and terminate/kill the python process that was used to start it.