diff --git a/README.md b/README.md index 36d9c34..5fb681f 100644 --- a/README.md +++ b/README.md @@ -595,6 +595,10 @@ sn list-swarms --seconds 5 See [`examples/`](./examples). ```bash +# smallest RPC example +python examples/basic_rpc/node.py +python examples/basic_rpc/client.py + # two nodes, one delegates to the other python examples/isolated_agents/agent_alpha.py python examples/isolated_agents/agent_beta.py diff --git a/example_node.py b/example_node.py deleted file mode 100644 index 78c7030..0000000 --- a/example_node.py +++ /dev/null @@ -1,17 +0,0 @@ -from synapse_p2p import Node - -app = Node(port=9999) - - -@app.periodic(3) -async def heartbeat(): - print("Running periodic task every 3 seconds") - - -@app.endpoint("sum") -async def sum_endpoint(a, b): - return a + b - - -if __name__ == "__main__": - app.run() diff --git a/examples/README.md b/examples/README.md index 6706f44..cb8aa8a 100644 --- a/examples/README.md +++ b/examples/README.md @@ -56,8 +56,8 @@ sn list-swarms ## 1. Basic node RPC ```bash -python example_node.py -python example_client.py +python basic_rpc/node.py +python basic_rpc/client.py ``` A node exposes `sum`. A client calls it. diff --git a/example_client.py b/examples/basic_rpc/client.py similarity index 84% rename from example_client.py rename to examples/basic_rpc/client.py index 9219e1f..0416492 100644 --- a/example_client.py +++ b/examples/basic_rpc/client.py @@ -5,7 +5,7 @@ async def main() -> None: result = await Client("127.0.0.1", 9999).call("sum", 1, 2) - print(f"Received: {result}") + print(f"sum(1, 2) = {result}") if __name__ == "__main__": diff --git a/examples/basic_rpc/node.py b/examples/basic_rpc/node.py new file mode 100644 index 0000000..8a44562 --- /dev/null +++ b/examples/basic_rpc/node.py @@ -0,0 +1,12 @@ +from synapse_p2p import Node + +node = Node(name="calculator", port=9999) + + +@node.endpoint("sum", description="Add two numbers") +async def sum_endpoint(a: int, b: int) -> int: + return a + b + + +if __name__ == "__main__": + node.run() diff --git a/pyproject.toml b/pyproject.toml index b9403bd..160c2c5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -67,5 +67,5 @@ asyncio_mode = "auto" testpaths = ["synapse_p2p/tests"] [tool.pyrefly] -project-includes = ["synapse_p2p", "examples", "example_client.py", "example_node.py"] +project-includes = ["synapse_p2p", "examples"] project-excludes = ["**/__pycache__", ".venv", ".ruff_cache", ".pytest_cache"]