Pyro4

4.82 · maintenance · verified Thu Apr 16

Pyro4 is a robust and mature distributed object middleware for Python, enabling remote method calls (RPC) between Python applications. It reached its last major release with 4.82 in August 2020 and is now in a maintenance-only mode, with active development having shifted to its successor, Pyro5. Pyro4 offers features like name server discovery, object exposure, and various serialization options, making it suitable for creating distributed systems, but users should be aware of its end-of-life status for new feature development.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic Pyro4 remote procedure call (RPC). It shows a server exposing a `GreetingMaker` object and a client connecting to it to call a remote method. For a fully distributed setup, you would typically run `pyro4-ns` (the Pyro4 Name Server) in a separate terminal, and both server and client would interact with it via `Pyro4.locateNS()` for discovery. This example simplifies by sharing the URI directly for self-contained execution.

import Pyro4
import threading
import time

# --- Server Code (run in a separate thread/process) ---
@Pyro4.expose
class GreetingMaker(object):
    def get_fortune(self, name):
        return f"Hello, {name}. This is your fortune from Pyro4!"

def run_server():
    # In a real scenario, you'd run 'pyro4-ns' in a separate terminal
    # For this example, we mock the Name Server interaction locally.
    # Or, you'd start the Name Server programmatically:
    # ns_thread = threading.Thread(target=Pyro4.naming.startNsLoop, daemon=True)
    # ns_thread.start()
    # time.sleep(1) # Give it a moment to start
    
    print("Pyro4 server starting...")
    daemon = Pyro4.Daemon()
    # We'll use a local registration for this quickstart without needing pyro4-ns to be separately run
    # In a real app, you'd use ns = Pyro4.locateNS() and ns.register()
    # For simplicity, we just register directly with the daemon and use its URI.
    uri = daemon.register(GreetingMaker, "example.greeting")
    print(f"Object registered with URI: {uri}")
    print("Server ready. Waiting for calls...")
    daemon.requestLoop()

# --- Client Code ---
def run_client(server_uri):
    print("Pyro4 client connecting...")
    try:
        # Use the URI obtained from the server directly
        with Pyro4.Proxy(server_uri) as greeter:
            print("Client got proxy.")
            print(greeter.get_fortune("World"))
            print(greeter.get_fortune("Pythonista"))
    except Exception as e:
        print(f"Client error: {e}")

if __name__ == "__main__":
    # To make this runnable as a single script for quickstart:
    # 1. Run the Name Server manually in a separate terminal: pyro4-ns
    # 2. Then, run this script. The server will register itself.
    # For this quickstart, we'll demonstrate a simplified local interaction
    # without explicitly starting a separate Name Server process within the script.
    # In a real distributed setup, `pyro4-ns` is essential.

    # Simplified local setup: Server registers object, client connects directly to its URI
    daemon = Pyro4.Daemon() # start a new Pyro daemon
    server_uri = daemon.register(GreetingMaker, "example.greeting") # register the object
    print(f"Server object URI: {server_uri}")

    server_thread = threading.Thread(target=daemon.requestLoop, daemon=True)
    server_thread.start()
    time.sleep(0.5) # Give the server a moment to start

    run_client(server_uri)

    print("Demonstration complete.")
    daemon.shutdown()

view raw JSON →