Skip to main content

A rapid RPC framework for building p2p networks

Project description

Synapse P2P

Synapse P2P is a small async Python RPC framework for building peer-to-peer services, local network tools, distributed workers, and lightweight service-to-service APIs.

It gives you a simple decorator-based server API, a built-in async client, structured MsgPack request/response messages, and length-prefixed TCP framing.

@app.endpoint("sum")
async def sum_endpoint(a, b):
    return a + b
result = await Client("127.0.0.1", 9999).call("sum", 1, 2)

Features

  • Async TCP RPC built on asyncio
  • MsgPack serialization for compact binary messages
  • Length-prefixed framing for reliable message boundaries over TCP
  • Decorator-based endpoints with @app.endpoint(...)
  • Built-in async client with request/response handling
  • Structured responses and errors via RPCResponse and RPCError
  • Positional and keyword arguments for remote calls
  • Request IDs for correlation and future persistent-connection support
  • Periodic background tasks with @app.background(...)
  • Serializer abstraction for custom protocols later
  • P2P-oriented primitives such as node identity and XOR-distance helpers

Installation

pip install synapse-p2p

For development with uv:

uv sync --group dev
uv run pytest

Quickstart

Create a server:

from synapse_p2p import Server

app = Server(address="127.0.0.1", port=9999)  # or Server() for defaults


@app.endpoint("sum")
async def sum_endpoint(a: int, b: int) -> int:
    return a + b


if __name__ == "__main__":
    app.run()

Call it from a client:

import asyncio

from synapse_p2p import Client


async def main() -> None:
    client = Client("127.0.0.1", 9999)
    result = await client.call("sum", 1, 2)
    print(result)


asyncio.run(main())

Output:

3

Server endpoints

Endpoints are async Python callables registered by name:

@app.endpoint("greet")
async def greet(name: str, excited: bool = False) -> str:
    message = f"Hello, {name}"
    return message.upper() if excited else message

Call with keyword arguments:

result = await client.call("greet", "Ada", excited=True)

Background tasks

Synapse can also run recurring async background jobs alongside your RPC server:

@app.background(5)
async def heartbeat():
    print("still alive")

The task above runs roughly every five seconds. Exceptions are logged and do not stop future runs.

Structured protocol

Synapse sends length-prefixed MsgPack messages over TCP.

A request looks like:

RPCRequest(
    id="request-id",
    endpoint="sum",
    args=[1, 2],
    kwargs={},
)

A successful response looks like:

RPCResponse(
    id="request-id",
    ok=True,
    result=3,
)

An error response looks like:

RPCResponse(
    id="request-id",
    ok=False,
    error=RPCError(code="bad_request", message="Unregistered endpoint called: nope"),
)

Low-level access

Most users should use Client, but lower-level message types are exported if you want to build custom transports or tooling:

from synapse_p2p import RPCError, RPCRequest, RPCResponse, RemoteProcedureCall
from synapse_p2p.framing import read_frame, write_frame
from synapse_p2p.serializers import MessagePackRPCSerializer

RemoteProcedureCall is kept as a backwards-compatible alias for RPCRequest.

Project status

Synapse is intentionally small and evolving. The current focus is a clean async RPC foundation. Future P2P-oriented work may include:

  • Peer discovery
  • Persistent connections
  • Routing tables / Kademlia-style node lookup
  • Handshakes and node capabilities
  • Authenticated or encrypted messages
  • Broadcast and gossip primitives

Keywords

Python RPC, asyncio RPC, peer-to-peer Python, P2P networking, MsgPack RPC, TCP RPC, async microservices, distributed workers, service-to-service communication.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

synapse_p2p-1.1.0.tar.gz (38.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

synapse_p2p-1.1.0-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file synapse_p2p-1.1.0.tar.gz.

File metadata

  • Download URL: synapse_p2p-1.1.0.tar.gz
  • Upload date:
  • Size: 38.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for synapse_p2p-1.1.0.tar.gz
Algorithm Hash digest
SHA256 3254a6575ad59a3c6fd8d7c7caa373e6657f6cc04800b81d051b0920f0bbc8d9
MD5 fa2ce8513997b2a521893b5086b5afc8
BLAKE2b-256 5aa9af768a77243c26f91565b7dbb68d68f6eab8536b4bb699ab4ae358b884b7

See more details on using hashes here.

File details

Details for the file synapse_p2p-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: synapse_p2p-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.15 {"installer":{"name":"uv","version":"0.11.15","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for synapse_p2p-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b1bf00b9e1380ebfa4748e7565449955bf02647e469b8be07a34225b899e0824
MD5 394f131444d415976bf24d87b824ff20
BLAKE2b-256 3e68da1c98149dcc3dca29dfb6a5928bbccacbaad71f45ffe555d065d55b0111

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page