Skip to content

Python RPC

Hero image

RPC (Remote Procedure Call) allows a program to execute a procedure in another program located on a remote machine or process, as if it were a local function. RPC protocol abstracts the network communication. It allows developers to focus on functionality without having to worry about low-level socket programming.

Common use cases for RPC:

  • Microservices Communication: interact with services in a distributed architecture,
  • Remote Execution: run tasks on remote machines,
  • Cross-language integration: enable communication between Python and systems based on other languages.

Python libraries for RPC:

  1. XML-RPC: built-in library,
  2. gRPC: framework developed by Google,
  3. Pyro4: Python Remote Objects supports more dynamic and Pythonic RPC features.

Pythonic describes code that uses the idioms, style, and conventions of the Python programming language in the way they are intended to be used, prioritizing readability, simplicity, and efficiency.

LibraryUsageProsCons
XML-RPCSmall applicationsBuilt-in and easy to useHas limited features and slow performance
gRPCMicroservices, high-performance, multi-language applicationsFast, efficient, streaming-based supportProtobuf required, complex setup
Pyro4Pythonic dynamic applicationsSimple syntax, supports Python objectsLess performance, limited to Python
  • Directorypython-project/
    • Directoryremote-procedure-call/
      • Directorygrpc/
        • simple-client.py calls method from stub generated class
        • simple-server.py exposes services from Protobuf configuration
        • calc.proto Protobuf configuration file
      • Directoryxml-rpc/
        • simple-client.py calls function from XML-RPC server
        • simple-server.py exposes a function to be called by XML-RPC clients

XML-RPC is a simple protocol that uses XML to encode requests and HTTP for communication. It’s included in Python’s standard library, making it a lightweight choice for basic RPC.

Open a terminal and run XML-RPC server:

Terminal window
cd python-project/remote-procedure-call/xml-rpc/
python simple-server.py

Open a second terminal and run XML-RPC client:

Terminal window
cd python-project/remote-procedure-call/xml-rpc/
python simple-client.py
Terminal window
# move into the correct directory
cd python-project/remote-procedure-call/grpc/
# create the virtual environment and activate it
python3 -m venv .venv/
source ./.venv/bin/activate
# install the dependencies
python3 -m pip install grpcio grpcio-tools

Generate Python code from calc.proto Protobuf file:

Terminal window
python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. calc.proto

Open a terminal and run gRPC server:

Terminal window
cd python-project/remote-procedure-call/grpc/
python simple-server.py

Open a second terminal and run gRPC client:

Terminal window
cd python-project/remote-procedure-call/grpc/
python simple-client.py
Terminal window
# move into the correct directory
cd python-project/remote-procedure-call/grpc/
# create the virtual environment and activate it
python3 -m venv .venv/
source ./.venv/bin/activate
# install the dependencies
python3 -m pip install Pyro4
``
Open a terminal and run **Pyro4** server:
```shell
cd python-project/remote-procedure-call/pyro4/
python simple-server.py

It will print the server’s URI, that you must input into the client.

Open a second terminal and run gRPC client:

Terminal window
cd python-project/remote-procedure-call/pyro4/
python simple-client.py

Insert the server URI when prompted and then click Enter keyboard button.

In my opinion, the most robust solution is gRPC, even though there’s a small learning curve on the first stage. By using Google’s Protobuf, you’re able to write cross-language applications.