|
2 | 2 | Remote Procedure Calls |
3 | 3 | ******************************************************************************** |
4 | 4 |
|
5 | | -* :mod:`compas.rpc` |
6 | | - |
7 | 5 | Remote Procedure Calls (``RPC``) is a mechanism to transparently execute code in |
8 | 6 | a remote process or computer. This is especially useful in scenarios where COMPAS |
9 | 7 | runs inside an IronPython host (eg. Rhino) and needs to execute code that only |
10 | 8 | runs on CPython (eg. code that requires ``numpy``). |
11 | 9 |
|
12 | | -COMPAS provides **two ways to achieve** this: ``rcp`` and ``XFunc``. |
13 | | - |
14 | | -Through ``Xfunc``, COMPAS provides a mechanism for calling Python functions through |
15 | | -a separately launched sub-process. |
16 | | - |
17 | | -A drawback of the ``Xfunc`` mechanism is that every call launches a new Python |
18 | | -(sub)process with all the overhead that that entails. For infrequent calls to |
19 | | -long-running processes this is not an issue. However, for frequent calls to functions |
20 | | -that are expected to run quickly, this is not ideal. |
21 | | - |
22 | | -The second mechanism is the ``rpc`` module. The principle of RPC is to start a server |
23 | | -that handles all requests. The advantage is that once the server is started, |
24 | | -no additional processes have to be launched and the server can handle the requests |
25 | | -without any overhead. Therefore, the response time is much faster than with ``XFunc``. |
26 | | - |
27 | 10 |
|
28 | 11 | Basic Usage |
29 | 12 | =========== |
@@ -64,6 +47,70 @@ The use of :mod:`compas.rpc` is not restricted to COMPAS packages only. :: |
64 | 47 | Note that Numpy arrays are automatically converted to lists. |
65 | 48 |
|
66 | 49 |
|
| 50 | +Configuration Options |
| 51 | +===================== |
| 52 | + |
| 53 | +The :class:`compas.rpc.Proxy` object has several configuration options. |
| 54 | +We will discuss only a few of those here. |
| 55 | +For a complete overview, please refer to the API docs (:mod:`compas.rpc`). |
| 56 | + |
| 57 | +``python`` |
| 58 | +---------- |
| 59 | + |
| 60 | +The :class:`compas.rpc.Proxy` object will automatically try to reconnect to an |
| 61 | +active instance of the command server, or start a new one if no active server can be found. |
| 62 | +By default, a newly started server will run an instance of the default Python interpreter |
| 63 | +of the active environment, for example, when running RPC from the command line; |
| 64 | +or of the Python interpreter specified in `compas_bootstrapper`, for example, when running RPC from Rhino. |
| 65 | + |
| 66 | +In some cases, this might not be what you want, or might not result in the expected behaviour, |
| 67 | +for example when `compas_bootstrapper` does not exist. |
| 68 | + |
| 69 | +To use a specific Python iterpreter, you can specify the path to an executable through the ``python`` parameter. |
| 70 | + |
| 71 | +.. code-block:: python |
| 72 | +
|
| 73 | + >>> from compas.rpc import Proxy |
| 74 | + >>> proxy = Proxy(python=r"C:\\Users\\<username>\\anaconda3\\envs\\research\\python.exe") |
| 75 | +
|
| 76 | +
|
| 77 | +``path`` |
| 78 | +-------- |
| 79 | + |
| 80 | +Sometimes you will want the server to run custom functions that are not (yet) part of a specific package. |
| 81 | +To allow the server to find such functions, you can specify an additional search path. |
| 82 | + |
| 83 | +For example, if you have a Python script on your desktop, |
| 84 | +defining a wrapper for the k-means clustering algorithm of ``scikit-learn``, |
| 85 | +you can tell the command server where to find it using the ``path`` parameter. |
| 86 | + |
| 87 | +.. code-block:: python |
| 88 | +
|
| 89 | + # C:\Users\<username>\Desktop\clustering.py |
| 90 | +
|
| 91 | + from sklearn.cluster import KMeans |
| 92 | + from numpy import array |
| 93 | +
|
| 94 | +
|
| 95 | + def cluster(points, n_clusters): |
| 96 | + kmeans = KMeans(n_clusters=n_clusters, n_init=2000, max_iter=1000).fit(array(cloud, dtype=float)) |
| 97 | + clusters = {} |
| 98 | + for label, point in zip(kmeans.labels_, cloud): |
| 99 | + if label not in clusters: |
| 100 | + clusters[label] = [] |
| 101 | + clusters[label].append(point) |
| 102 | + return clusters |
| 103 | +
|
| 104 | +
|
| 105 | +.. code-block:: python |
| 106 | +
|
| 107 | + >>> from compas.geometry import Pointcloud |
| 108 | + >>> from compas.rpc import Proxy |
| 109 | + >>> cloud = Pointcloud.from_bounds(10, 5, 3, 100) |
| 110 | + >>> proxy = Proxy(package='clustering', path=r'C:\\Users\\<username>\\Desktop') |
| 111 | + >>> clusters = proxy.cluster(cloud, 10) |
| 112 | +
|
| 113 | +
|
67 | 114 | Supported data types |
68 | 115 | ==================== |
69 | 116 |
|
|
0 commit comments