RPyC (Remote Python Call)

6.0.2 · active · verified Sun Apr 12

RPyC (Remote Python Call) is a transparent and symmetric distributed computing library for Python. It allows remote objects to be manipulated as if they were local, making it a powerful tool for building distributed systems and remote execution. The library is actively maintained, with frequent releases addressing bug fixes, performance improvements, and security enhancements. The current version is 6.0.2.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a simple RPyC client-server interaction. First, save the server code as `server.py` and run it in a terminal. Then, save the client code as `client.py` and run it in another terminal to connect to the server, call a remote method, and access a remote module.

# server.py
import rpyc
from rpyc.utils.server import ThreadedServer

class MathService(rpyc.Service):
    def on_connect(self, conn):
        print("Client connected!")

    def on_disconnect(self, conn):
        print("Client disconnected!")

    def exposed_fib(self, n):
        """Calculates the Fibonacci sequence up to n."""
        seq = []
        a, b = 0, 1
        while a < n:
            seq.append(a)
            a, b = b, a + b
        return seq

if __name__ == '__main__':
    # Using a fixed port for demonstration
    port = 18812
    print(f"Starting MathService on port {port}...")
    ts = ThreadedServer(MathService, port=port)
    ts.start()

# --- Save the above as server.py and run: python server.py ---

# client.py
import rpyc
import time

def run_client():
    host = "localhost" # Replace with your server's address if remote
    port = 18812
    try:
        conn = rpyc.connect(host, port)
        print(f"Connected to RPyC server at {host}:{port}")

        # Access an exposed method on the root object
        result = conn.root.fib(1000)
        print(f"Fibonacci sequence up to 1000: {result}")

        # Example of accessing a remote module (classic RPyC behavior)
        remote_sys_version = conn.modules.sys.version
        print(f"Remote Python version: {remote_sys_version.splitlines()[0]}")

        conn.close()
        print("Connection closed.")
    except ConnectionRefusedError:
        print(f"Error: Connection refused. Is the server running on {host}:{port}?")
    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == '__main__':
    # Optional: Give the server a moment to start if run immediately after
    # time.sleep(1)
    run_client()

view raw JSON →