Seminar 1: Remote Procedure Calls (RPC)
Goal: To understand Remote Procedure Calls (RPC) in Python.
Definitions: Remote Procedure Call (RPC) is a protocol that one program can use to request a service from a program located in another computer (or node) on a network without having to understand the network’s details.
- A message based communication scheme should be used to provide remote service as the processes are executing on separate systems.
- The messages exchanged in RPC communication are well structured and are thus no longer just packets of data.
- Each message is addressed to an RPC daemon listening to a port on the remote system, and each contains an identifier of the function to execute and the parameters to pass to that function. The function is then executed as requested, and any output is sent back to the requester in a separate message.
- Using stub function to marshal the argument and send the message to the remote server.
Prerequisites: Python (Python Download), "RPyC" (RPyC Installation).
- Python installation: https://realpython.com/installing-python/
Exercise: We will get acquainted with RPC procedure by using RPyC in Python. RPyC (Remote Python Call) is a transparent python library for symmetrical remote procedure calls, clustering and distributed-computing. RPyC makes use of object-proxying, a technique that employs python’s dynamic nature, to overcome the physical boundaries between processes and computers, so that remote objects can be manipulated as if they were local.
- In RPyC, the client sends the request to the server for processing, and the server then receives the request and executes the code which looks like being manipulated locally. Then the result is sent back to the client by messaging passing.
- The process is implemented in a single machine, but specifies two separate instances, one for the client, and one for the server.
- The code for the above implementation is given here for you. (Code)
- Server: rpyc_classic.py. In this classic mode, the server was completely under the control of its client – there was no way to restrict what the client could do, and there was no notion of services. A client simply connected to a server and began to manipulate it.
- Client: rpyc_client.py.
WORKING PIPELINE OF THE ABOVE IMPLEMENTATION:
Install RPyC
$ pip install rpyc
Running a Server
$ python rpyc_classic.py INFO:SLAVE/18812:server started on [127.0.0.1]:18812
- [127.0.0.1]:18812 is the address on which the server binds, in this case the server will only accept connections from localhost. If you use the same computer to run both client and server, use 127.0.0.1 as the IP Address
Running a Client
$ python rpyc_client.py localhost
- In rpyc_client.py
# create a connection to the server: then we can use the connection object stored in conn to execute commands on the remote server. $ conn = rpyc.classic.connect(server) # access remote modules: then we can use the rsys on the client just as we would use sys on the server $ rsys = conn.modules.sys $ print(rsys.version) # execute: execute arbitrary code on the server. $ conn.execute('scores = { "Foo" : 10 }') $ conn.execute('scores["Foo"] += 1') $ conn.execute('scores["Bar"] = 42') # eval: evaluate an expression and return the results $ local_scores = conn.eval('scores') # we can use the namespace attribute of the connection object which is a dictionary where the keys are the object in on the remote server $ conn.namespace["scores"]["Bar"] += 58
Task: in the rpyc-client.py, define a function (bubble sort, quick sort and so on) and use the methods (execute, eval, namespace and so on) of the connection object to run the function on the remote server.
Deliverables: Zip file containing the source code file and the screenshots of the server and the client.
Link:
NOTE: Watch the video if something is not clear.