Skip to content

SRPC is a lightweight Remote Procedure Call library designed for clean abstraction between distributed services or processes, facilitating rapid prototyping.

Notifications You must be signed in to change notification settings

oseasandrepro/RPC-LIB

Repository files navigation

Simple RPC Library (SRPC)

SRPC is a lightweight Remote Procedure Call (RPC) library that provides a clean abstraction layer between distributed processes,
enabling rapid development and integration of distributed services.

Table of Contents

1. Features

  • Simple API for exposing and calling remote procedures.
  • Interface definition through Python conventions (no separate IDL required).
  • Plug-and-play architecture — install and start implementing immediately.
  • Remote exception propagation
    • Serialization: Convert server-side exceptions.
    • Rehydration: Raise equivalent exceptions on the client side.
  • Stub generation for server and client.
  • Live metrics dashboard, displaying call count and latency per procedure.

2. How to Use the Library

Strict naming conventions are required for the plug-and-play functionality.

1. Download and Install the library

  1. Download the latest release from the GitHub Releases page.
  2. Extract the archive
  3. Open the terminal inside srpcLib-<version> directory.
  4. To install the package run the command: pip install .

2. Implementing a Distributed Calculator using SRPC

Let’s build a simple distributed calculator service that supports the four basic arithmetic operators.
(Addition, Subtraction, Multiplication, Division)

Server Directory Structure

project/
├─ calc/
│  ├─ __init__.py
│  ├─ calc_interface.py
│  ├─ calc.py
├─ srpc_calc_server_stub.py
├─ server.py
...
  • calc directory name, is the name of the package, that the server will serve
  • calc_interface.py is the service interface
  • calc.py is the service implementation

Both the service-package directory and the service-package.py file should have the same name.
I recommend only use lowercase for it.

We need to implement calc_interface.py, calc.py and server.py.
Copy the following code:

# calc_interface.py
from abc import ABC, abstractmethod

# The interface class name must be <package-name>Interface. 
# The first character must be uppercase.
class CalcInterface(ABC):
    @abstractmethod
    def add(self, a: int, b: int) -> int:
        pass

    @abstractmethod
    def sub(self, a: int, b: int) -> int:
        pass

    @abstractmethod
    def mult(self, a: int, b: int) -> int:
        pass

    @abstractmethod
    def div(self, a: int, b: int) -> int:
        pass
# calc.py
from .calc_interface import CalcInterface

# The service class name must be <package-name>. 
# The first character must be uppercase.
class Calc(CalcInterface):
    def add(self, a: int, b: int) -> int:
        return a + b

    def sub(self, a: int, b: int) -> int:
        return a - b

    def mult(self, a: int, b: int) -> int:
        return a * b

    def div(self, a: int, b: int) -> int:
        return a / b
# server.py
from srpc_calc_server_stub import SrpcCalcServerStub

calcServerStub = SrpcCalcServerStub()
calcServerStub.start()

Client Directory Structure

project/
├─ calc/
│  ├─ __init__.py
│  ├─ calc_interface.py
├─ srpc_calc_client_stub.py
├─ client.py
...

We need implement calc_interface.py and client.py.
Copy the following code:

# calc_interface.py
from abc import ABC, abstractmethod

# The interface class name must be <package-name>Interface. 
# The first character must be in uppercase.
class CalcInterface(ABC):
    @abstractmethod
    def add(self, a: int, b: int) -> int:
        pass

    @abstractmethod
    def sub(self, a: int, b: int) -> int:
        pass

    @abstractmethod
    def mult(self, a: int, b: int) -> int:
        pass

    @abstractmethod
    def div(self, a: int, b: int) -> int:
        pass
# client.py
from srpc_calc_client_stub import SrpcCalcClientStub

SERVER_HOST = "127.0.0.1" # You should use your LAN IP
calcStub = SrpcCalcClientStub(SERVER_HOST)

a = 4
b = 2

print(f"{calcStub.add(a, b)}")
print(f"{calcStub.mult(a, b)}")
print(f"{calcStub.sub(a, b)}")
print(f"{calcStub.div(a, b)}")

Generate the Stubs

The tool to generate the stubs is srpc_stub_gen.
Inside the server directory run the command:
python -m srpcLib.tools.srpc_stub_gen calc/calc_interface.py

This will generate two files srpc_calc_server_stub.py and srpc_calc_client_stub.py.

  • srpc_calc_server_stub.py is the server-side stub
  • srpc_calc_client_stub.py is the client-side stub

Move the srpc_calc_client_stub.py file to client directory.

Running the Server and Client

The server uses 127.0.0.1 by default.
If your machine is connected to a LAN, it will automatically use your LAN IP address.

To check your IP address:

  • Windows: ipconfig
  • Linux: ifconfig

Start the server:
Run this command inside the server directory:

python server.py

You should see something like this:
alt text

Start the client:
Run this command inside the client directory:

python client.py

3. Using the live metrics dashbord

The srpc_show_metrics allows you to see call count(sucess or fail) and latency per procedure, in real time.
To view it, run this command inside the server directory:

python -m srpcLib.tools.srpc_show_metrics srpc_server_metrics.log

You should see something like this: alt text

In another terminal window, go inside the client directory and run the command: python client.py — two times.
Now back to srpc_show_metrics terminal window, you should see something like this:
alt text

3. What is Remote Procedure Call (RPC)

In distributed computing, a remote procedure call (RPC) is when a computer program causes a procedure
to execute in a different address space of the current process (commonly on another computer on a shared computer network),
which is written as if it were a local procedure call, without the programmer explicitly writing the
details for the remote interaction. The programmer writes essentially the same code whether
the subroutine is local to the executing program, or remote.(Wikipedia contributors, 2025)

Remote Procedure Call Flow

The diagram below illustrates the RPC flow between client and server:

+-----------------------------+            +------------------------------+
|           CLIENT            |            |            SERVER            |
+-----------------------------+            +------------------------------+
| User Code                   |            |                              |
| ------------------------->  | 1. Call RPC stub with method + args       |
| RPC Stub / Proxy            |            |                              |
| ------------------------->  | 2. Prepare request object                 |
| Serializer (byte buffer)    |            |                              |
| ------------------------->  | 3. Serialize method & args                |
| Transport Layer (TCP/IP)    | ---------> | 4. Receive request bytes     |
|                             |            | Transport Layer              |
|                             |            | -------------------------->  |
|                             |            | Deserialize request          |
|                             |            | Serializer                   |
|                             |            | -------------------------->  |
|                             |            | Extract method + args        |
|                             |            | RPC Dispatcher               |
|                             |            | -------------------------->  |
|                             |            | Locate & invoke target fn    |
|                             |            | Target Function              |
|                             |            | -------------------------->  |
|                             |            | Return result                |
|                             |            | Serializer                   |
| <-------------------------- | <----------| Serialize return value       |
| Deserializer                |            |                              |
| <-------------------------- |            |                              |
| RPC Stub                    |            |                              |
| Return result to user       |            |                              |
+-----------------------------+            +------------------------------+

4. Final Thoughts

With SRPC, you can rapidly configure and consume interface-oriented RPC services with minimal setup and native Python code.

WARNING: No performance tests or rigorous security analysis have been conducted. Do not use this in production environments.

Expansion Possibilities:

  • Performance Benchmarking
  • Security Review
  • Authentication & Authorization Layers

For more details on the current project stage, read the last release notes, here: V0.0.0

License

MIT

About

SRPC is a lightweight Remote Procedure Call library designed for clean abstraction between distributed services or processes, facilitating rapid prototyping.

Topics

Resources

Stars

Watchers

Forks

Languages