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)

Server lifecycle

Use app.run() for simple scripts. In larger async applications or tests, use start() and stop():

server = await app.start()
try:
    ...
finally:
    await app.stop()

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.2.0.tar.gz (39.9 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.2.0-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: synapse_p2p-1.2.0.tar.gz
  • Upload date:
  • Size: 39.9 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.2.0.tar.gz
Algorithm Hash digest
SHA256 db2c8ed78b3d11785760fb923eb7eda2a15fe09b49f73c89a3b36b66f2a584ac
MD5 96c49d5a14ff59a6d2804de22831d636
BLAKE2b-256 553dfff40f72fbbffe010ad080c1113e08bcd5c682efdfa66ce691887139f221

See more details on using hashes here.

File details

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

File metadata

  • Download URL: synapse_p2p-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 15.3 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1bd4782e0325a0a03e28d466f207e2ff4189c8ad069a390ed85e1ea1caf31b97
MD5 a3184997e18432d36a55cdbec31e7c3e
BLAKE2b-256 ad5ec2e18215865164c93090020189bbb5129eba974332e4baf1a8364646a6d8

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