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. Default is 'pythonw'.

  • url (str:, optional) – The server address. Default is 'http://127.0.0.1'.

  • port (int:, optional) – The port number on the remote server. Default is 1753.

  • service (str:, 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, True to 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, True to capture the stdout/stderr output of the remote process, otherwise False.) – In general, capture_output should be True when using a pythonw as executable (default).

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

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.