Proxy

class compas.rpc.Proxy[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:
packagestr, optional

The base package for the requested functionality. Default is None, in which case a full path to function calls should be provided.

pythonstr, optional

The python executable that should be used to execute the code.

urlstr, optional

The server address.

portint, optional

The port number on the remote server.

servicestr, 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.

autoreloadbool, 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_outputbool, 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).

pathstr, optional

Path to the folder containing the module to be proxied. This is useful for cases where the module to be proxied is not on the PYTHONPATH.

working_directorystr, optional

Current working directory for the process that will be started to run the server. This is useful for cases where a custom service is used and the service is not on the PYTHONPATH.

Attributes:
addressstr, read-only

Address of the server as a combination of url and port.

profilestr

A profile of the code executed by the server.

packagestr

Fully qualified name of the package or module from where functions should be imported on the server side.

servicestr

Fully qualified package name required for starting the server/service.

pythonstr

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.