Published on

RPC(Remote Procedure Call) with Python example

Authors
  • avatar
    Name
    Gene Zhang
    Twitter

RPC

A Remote Procedure Call (RPC) allows a program to execute a procedure on another computer or server as if it were a local function call, abstracting away the complexities of network communication.

from xmlrpc.server import SimpleXMLRPCServer

def add(a, b):
    return a + b

def multiply(a, b):
    return a * b

server = SimpleXMLRPCServer(("localhost", 8000))
print("Listening on port 8000...")
server.register_function(add, "add")
server.register_function(multiply, "multiply")
server.serve_forever()

import xmlrpc.client

with xmlrpc.client.ServerProxy("http://localhost:8000/") as proxy:
    print("3 + 5 = %d" % proxy.add(3, 5))
    print("3 * 5 = %d" % proxy.multiply(3, 5))

gRPC

RPC vs gRPC

FeaturegRPCRPC (General)
Data SerializationProtocol Buffers (binary)Various (JSON, XML, Protobuf)
Transport ProtocolHTTP/2 (primarily)Varies (TCP, UDP, HTTP)
PerformanceGenerally fasterVaries depending on implementation
StreamingSupports streamingMay or may not support streaming
ComplexitySteeper learning curveVaries, can be simpler
Language SupportGood support for many languagesVaries, depends on implementation
MicroservicesWell-suited for microservicesCan be used for microservices, but may not be the best choice

In essence: gRPC is a modern, high-performance RPC framework built for efficient communication, especially in distributed systems and microservices. While traditional RPC is a broader concept, it can be less performant than gRPC due to its reliance on potentially less efficient data formats and transport protocols.