Skip to main content

bare-rpc-python

RPC over any byte transport for Python. The Python counterpart of bare-rpc, and wire compatible with it. Built on compact-encoding-python.

pip install bare-rpc

hrpc-python generates typed clients and servers on top of it. Use it directly for the raw transport.

Wire codec

Encode and decode frames without the runtime:

import bare_rpc as rpc

frame = rpc.encode_request(1, 42, data=b"hi")  # -> bytes
msg = rpc.decode_frame(frame)  # -> RequestMessage(id=1, command=42, ...)

RPC runtime

RPC runs a session over a Transport: Any object with an async read(), returning b"" once it has ended, an async write(data), and close(). A Sidecar from bare-sidecar-python is one:

import asyncio

from bare_rpc import RPC
from bare_sidecar import spawn


async def main():
    sidecar = await spawn("app.js")

    async with RPC(sidecar) as rpc:
        print(await rpc.request(1, b"ping"))

        await rpc.event(2, b"note")


asyncio.run(main())

Where app.js serves the requests with bare-rpc:

const RPC = require("bare-rpc");

new RPC(Bare.IPC, (req) => req.reply("pong"));

For a socket or any other asyncio stream, wrap its reader and writer in a StreamTransport:

import asyncio

from bare_rpc import RPC, StreamTransport


async def main():
    reader, writer = await asyncio.open_connection("localhost", 8080)

    async with RPC(StreamTransport(reader, writer)) as rpc:
        print(await rpc.request(1, b"ping"))

The session closes once the transport ends, and closing it closes the transport. Pass on_request and on_event to answer requests and receive events from the peer:

async def on_request(req):
    await req.reply(b"pong")


rpc = RPC(transport, on_request=on_request)

Command routing

CommandRouter sends each request and event to the handler registered for its command. Pass its dispatch_request and dispatch_event to RPC:

router = rpc.CommandRouter()


async def ping(req):
    return b"pong"


router.on_request(5, ping)
router.on_event(3, lambda event: print(event.data))

server = rpc.RPC(
    transport,
    on_request=router.dispatch_request,
    on_event=router.dispatch_event,
)

Streams and backpressure

All three streaming shapes are implemented, with backpressure.

Incoming streams are read with async for, or a chunk at a time with await stream.read(), which returns None once the peer has ended the stream.

Response stream (server streams back, client reads):

async def on_request(req):
    out = await req.create_response_stream()
    await out.write(b"chunk-1")
    await out.write(b"chunk-2")
    await out.end()


incoming = await client.request_with_response_stream(command=5, data=b"go")
async for chunk in incoming:
    ...

Request stream (client streams up, server reads, then replies):

async def on_request(req):
    async for chunk in req.request_stream:
        ...
    await req.reply(b"done")


out, reply = await client.stream_request(command=6)
await out.write(b"chunk-1")
await out.end()
result = await reply  # -> b"done"

Duplex (both directions):

out, incoming = await client.create_bidirectional_stream(command=7)
await out.write(b"ping")
await out.end()
async for chunk in incoming:
    ...

License

Apache-2.0

Metadata

Release files for bare-rpc 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bare-rpc 0.1.0
File Size Uploaded Provenance Actions
bare_rpc-0.1.0.tar.gz 24.9 kB Without provenance Details for bare_rpc-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bare-rpc 0.1.0
File Interpreter ABI Platform Actions
bare_rpc-0.1.0-py3-none-any.whl Python 3 none any Details for bare_rpc-0.1.0-py3-none-any.whl

Total release size: 42.8 kB

Release files / bare_rpc-0.1.0.tar.gz

Download URL bare_rpc-0.1.0.tar.gz
Size 24.9 kB
Tags Source
SHA-256 checksum
How to use checksums
3cdb82578e1e79e7ef3d95216386f09ae656095f00de15f4288819b36623cc95
BLAKE2b-256 checksum
How to use checksums
f86ed3c230a37f1d7afee1eaf78097616c822414c44434d4e92abc800a90d1b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.12.15

Release files / bare_rpc-0.1.0-py3-none-any.whl

Download URL bare_rpc-0.1.0-py3-none-any.whl
Size 17.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e8be0aa0a693f28373cebbd3302f82d7f5cbf82b5174eb29848a7b65c4e67c2e
BLAKE2b-256 checksum
How to use checksums
5a400f6dad2e78c395bf443f3b41a21fc22f9e9ec56e763090e29cde6bc0020a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.12.15

Security for bare-rpc 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page