API Reference¶
Protocol & Tuning¶
- class grpclib_transports.TransportTuning(buffer_size: int, read_chunk_size: int, write_high_water: int, write_low_water: int, http2_stream_window_size: int, http2_connection_window_size: int, http2_max_frame_size: int, transfer_chunk_size: int)[source]¶
Buffer, window, and chunk size knobs for transport performance.
All values are in bytes. Create a tuned instance with
from_env()or read the pre-computedDEFAULT_TUNING.- Fields:
buffer_size: OS pipe buffer size used when bumping subprocess pipes. read_chunk_size: Size of each
read()call in the pump loop. write_high_water: High-water mark for write buffer limits. write_low_water: Low-water mark for write buffer limits. http2_stream_window_size: Per-stream HTTP/2 flow-control window. http2_connection_window_size: Connection-level HTTP/2 flow-control window. http2_max_frame_size: Maximum HTTP/2 frame size sent to the peer. transfer_chunk_size: Default chunk size for file/data chunk iterators.
- grpclib_transports.DEFAULT_TUNING = TransportTuning(buffer_size=8388608, read_chunk_size=8388608, write_high_water=8388608, write_low_water=4194304, http2_stream_window_size=16777216, http2_connection_window_size=67108864, http2_max_frame_size=1048576, transfer_chunk_size=262144)¶
Buffer, window, and chunk size knobs for transport performance.
All values are in bytes. Create a tuned instance with
from_env()or read the pre-computedDEFAULT_TUNING.- Fields:
buffer_size: OS pipe buffer size used when bumping subprocess pipes. read_chunk_size: Size of each
read()call in the pump loop. write_high_water: High-water mark for write buffer limits. write_low_water: Low-water mark for write buffer limits. http2_stream_window_size: Per-stream HTTP/2 flow-control window. http2_connection_window_size: Connection-level HTTP/2 flow-control window. http2_max_frame_size: Maximum HTTP/2 frame size sent to the peer. transfer_chunk_size: Default chunk size for file/data chunk iterators.
- class grpclib_transports.PeerIdentity(transport: str, username: str | None, uid: int | None = None, gid: int | None = None, pid: int | None = None, group_ids: tuple[int, ...] | None = None, group_names: tuple[str, ...] | None = None)[source]¶
Identity information about the remote peer of a transport.
- Fields:
transport: Transport type (
"stdio","ssh","unix","unknown"). username: OS username of the peer, if available. uid: Unix user ID of the peer. gid: Unix group ID of the peer. pid: Process ID of the peer. group_ids: Supplementary group IDs. group_names: Supplementary group names (resolved from group_ids).
- grpclib_transports.local_process_identity(*, transport: str) PeerIdentity[source]¶
Build a
PeerIdentityfor the current process.
- grpclib_transports.peer_identity_from_transport(transport: Any) PeerIdentity[source]¶
Extract
PeerIdentityfrom an asyncio transport.Checks for a stored
peer_identityextra, an SSH username, or a Unix-domain socket withSO_PEERCRED.
- grpclib_transports.peer_identity_from_stream(stream: Any) PeerIdentity[source]¶
Extract
PeerIdentityfrom a gRPC stream's underlying transport.
- grpclib_transports.make_h2_config(*, client_side: bool) H2Configuration[source]¶
Build an
h2configuration with strict validation off.Disables inbound/outbound header validation and normalization so protobuf-based headers pass through unchanged.
- grpclib_transports.make_config(tuning: TransportTuning = TransportTuning(buffer_size=8388608, read_chunk_size=8388608, write_high_water=8388608, write_low_water=4194304, http2_stream_window_size=16777216, http2_connection_window_size=67108864, http2_max_frame_size=1048576, transfer_chunk_size=262144)) Configuration[source]¶
Build a grpclib
Configurationwith tuned window sizes.
- grpclib_transports.make_server_protocol(mapping: dict[str, Handler], *, tuning: TransportTuning = TransportTuning(buffer_size=8388608, read_chunk_size=8388608, write_high_water=8388608, write_low_water=4194304, http2_stream_window_size=16777216, http2_connection_window_size=67108864, http2_max_frame_size=1048576, transfer_chunk_size=262144), status_details_codec: StatusDetailsCodecBase | None = None) H2Protocol[source]¶
Build a server-side H2 protocol with the given handler mapping.
status_details_codecis what lets a handler'sGRPCError.detailsreach the client: grpclib encodes it into thegrpc-status-details-bintrailer only when a codec is configured, and drops it silently otherwise. The client's channel must be given the same codec to decode it.
- grpclib_transports.init_h2_transport(protocol: H2Protocol, transport: Any, *, tuning: TransportTuning = TransportTuning(buffer_size=8388608, read_chunk_size=8388608, write_high_water=8388608, write_low_water=4194304, http2_stream_window_size=16777216, http2_connection_window_size=67108864, http2_max_frame_size=1048576, transfer_chunk_size=262144)) None[source]¶
Wire an H2 protocol to a transport and advertise a tuned max frame size.
- grpclib_transports.build_mapping(handlers: Sequence[IServable]) dict[str, Handler][source]¶
Merge
__mapping__()from a sequence of servable handlers into one dict.
- async grpclib_transports.pump(protocol: H2Protocol, reader: Any, *, tuning: TransportTuning = TransportTuning(buffer_size=8388608, read_chunk_size=8388608, write_high_water=8388608, write_low_water=4194304, http2_stream_window_size=16777216, http2_connection_window_size=67108864, http2_max_frame_size=1048576, transfer_chunk_size=262144)) None[source]¶
Read from a byte stream and feed data into an H2 protocol.
Blocks in a loop calling
reader.read(). On EOF or error, callsprotocol.connection_lost().
- async grpclib_transports.serve_h2(handlers: Sequence[IServable], reader: Any, transport: Any, *, tuning: TransportTuning = TransportTuning(buffer_size=8388608, read_chunk_size=8388608, write_high_water=8388608, write_low_water=4194304, http2_stream_window_size=16777216, http2_connection_window_size=67108864, http2_max_frame_size=1048576, transfer_chunk_size=262144), max_concurrency: int | None = None, status_details_codec: StatusDetailsCodecBase | None = None) None[source]¶
Build a server protocol, wire it to a transport, and pump frames.
- grpclib_transports.pause_h2_protocol(protocol: BaseProtocol | None) None[source]¶
Pause writing on an H2 protocol, if set.
- grpclib_transports.resume_h2_protocol(protocol: BaseProtocol | None) None[source]¶
Resume writing on an H2 protocol, if set.
Transports¶
- class grpclib_transports.BaseCustomTransport[source]¶
Bases:
TransportBase class for custom asyncio transports that wrap a reader/writer pair.
Concrete transports (StdioTransport, SshTransport) override: -
write()— forward data to the underlying writer -get_extra_info()— delegate to the appropriate underlying object -get_write_buffer_size()— introspect the write buffer (for flow control) -abort()— hard reset (transport-specific) -can_write_eof()/write_eof()— EOF support (transport-specific)Flow-control bridging (pause_writing/resume_writing forwarding to the H2Protocol) is handled by each concrete transport's own mechanism.
- pause_reading() None[source]¶
Pause the receiving end.
No data will be passed to the protocol's data_received() method until resume_reading() is called.
- class grpclib_transports.StdioTransport(reader: StreamReader, writer: StreamWriter, *, tuning: TransportTuning = TransportTuning(buffer_size=8388608, read_chunk_size=8388608, write_high_water=8388608, write_low_water=4194304, http2_stream_window_size=16777216, http2_connection_window_size=67108864, http2_max_frame_size=1048576, transfer_chunk_size=262144))[source]¶
Bases:
BaseCustomTransportAn asyncio transport that wraps a stdio subprocess pipe pair.
- abort() None[source]¶
Close the transport immediately.
Buffered data will be lost. No more data will be received. The protocol's connection_lost() method will (eventually) be called with None as its argument.
- close() None[source]¶
Close the transport.
Buffered data will be flushed asynchronously. No more data will be received. After all buffered data is flushed, the protocol's connection_lost() method will (eventually) be called with None as its argument.
- class grpclib_transports.SshTransport(reader: Any, writer: Any, *, tuning: TransportTuning = TransportTuning(buffer_size=8388608, read_chunk_size=8388608, write_high_water=8388608, write_low_water=4194304, http2_stream_window_size=16777216, http2_connection_window_size=67108864, http2_max_frame_size=1048576, transfer_chunk_size=262144))[source]¶
Bases:
BaseCustomTransportAn asyncio transport that wraps an asyncssh channel.
Forwards asyncssh session
pause_writing/resume_writingcallbacks to the H2 protocol for end-to-end backpressure. Restores the original session callbacks on close or abort.- abort() None[source]¶
Close the transport immediately.
Buffered data will be lost. No more data will be received. The protocol's connection_lost() method will (eventually) be called with None as its argument.
- close() None[source]¶
Close the transport.
Buffered data will be flushed asynchronously. No more data will be received. After all buffered data is flushed, the protocol's connection_lost() method will (eventually) be called with None as its argument.
- set_write_buffer_limits(high: int | None = None, low: int | None = None) None[source]¶
Set the high- and low-water limits for write flow control.
These two values control when to call the protocol's pause_writing() and resume_writing() methods. If specified, the low-water limit must be less than or equal to the high-water limit. Neither value can be negative.
The defaults are implementation-specific. If only the high-water limit is given, the low-water limit defaults to an implementation-specific value less than or equal to the high-water limit. Setting high to zero forces low to zero as well, and causes pause_writing() to be called whenever the buffer becomes non-empty. Setting low to zero causes resume_writing() to be called only once the buffer is empty. Use of zero for either limit is generally sub-optimal as it reduces opportunities for doing I/O and computation concurrently.
- class grpclib_transports.PipeTransport(reader: StreamReader, writer: StreamWriter, *, transport_name: str = 'pipe', tuning: TransportTuning = TransportTuning(buffer_size=8388608, read_chunk_size=8388608, write_high_water=8388608, write_low_water=4194304, http2_stream_window_size=16777216, http2_connection_window_size=67108864, http2_max_frame_size=1048576, transfer_chunk_size=262144))[source]¶
Bases:
BaseCustomTransportAn asyncio transport that wraps a raw OS pipe pair.
Differs from
StdioTransportin that it accepts arbitrary binary file objects rather than being hardwired tosys.stdin/sys.stdout.- abort() None[source]¶
Close the transport immediately.
Buffered data will be lost. No more data will be received. The protocol's connection_lost() method will (eventually) be called with None as its argument.
- close() None[source]¶
Close the transport.
Buffered data will be flushed asynchronously. No more data will be received. After all buffered data is flushed, the protocol's connection_lost() method will (eventually) be called with None as its argument.
Channels¶
- class grpclib_transports.StdioChannel(reader: Any, writer: Any, *, transport: StdioTransport | None = None, tuning: TransportTuning = TransportTuning(buffer_size=8388608, read_chunk_size=8388608, write_high_water=8388608, write_low_water=4194304, http2_stream_window_size=16777216, http2_connection_window_size=67108864, http2_max_frame_size=1048576, transfer_chunk_size=262144), **kwargs: Any)[source]¶
Bases:
ChannelA gRPC channel that speaks H2 over a subprocess stdio pipe pair.
- class grpclib_transports.SshChannel(reader: Any, writer: Any, *, tuning: TransportTuning = TransportTuning(buffer_size=8388608, read_chunk_size=8388608, write_high_water=8388608, write_low_water=4194304, http2_stream_window_size=16777216, http2_connection_window_size=67108864, http2_max_frame_size=1048576, transfer_chunk_size=262144), **kwargs: Any)[source]¶
Bases:
ChannelA gRPC channel that speaks H2 over an asyncssh session.
- class grpclib_transports.PipeChannel(reader: Any, writer: Any, *, transport: PipeTransport | None = None, tuning: TransportTuning = TransportTuning(buffer_size=8388608, read_chunk_size=8388608, write_high_water=8388608, write_low_water=4194304, http2_stream_window_size=16777216, http2_connection_window_size=67108864, http2_max_frame_size=1048576, transfer_chunk_size=262144), **kwargs: Any)[source]¶
Bases:
ChannelA gRPC channel that speaks H2 over a raw OS pipe pair.
Server¶
- class grpclib_transports.Server(*, tuning: TransportTuning = TransportTuning(buffer_size=8388608, read_chunk_size=8388608, write_high_water=8388608, write_low_water=4194304, http2_stream_window_size=16777216, http2_connection_window_size=67108864, http2_max_frame_size=1048576, transfer_chunk_size=262144))[source]¶
Bases:
objectContainer for multiple service endpoints and worker managers.
- async grpclib_transports.serve_stdio(handlers: list[IServable], *, tuning: TransportTuning = TransportTuning(buffer_size=8388608, read_chunk_size=8388608, write_high_water=8388608, write_low_water=4194304, http2_stream_window_size=16777216, http2_connection_window_size=67108864, http2_max_frame_size=1048576, transfer_chunk_size=262144), max_concurrency: int | None = None, status_details_codec: StatusDetailsCodecBase | None = None) None[source]¶
Serve gRPC over the current process's stdin/stdout.
Redirects stdout to stderr before starting so that only H2 frames go over the pipe.
- async grpclib_transports.serve_ssh(handlers: list[IServable], host: str = '127.0.0.1', port: int = 8022, *, tuning: TransportTuning = TransportTuning(buffer_size=8388608, read_chunk_size=8388608, write_high_water=8388608, write_low_water=4194304, http2_stream_window_size=16777216, http2_connection_window_size=67108864, http2_max_frame_size=1048576, transfer_chunk_size=262144), max_concurrency: int | None = None, status_details_codec: StatusDetailsCodecBase | None = None) None[source]¶
Start an asyncssh server that speaks H2 on each session.
Generates an ephemeral Ed25519 key and accepts connections indefinitely. Shuts down gracefully on SIGINT or SIGTERM.
Client¶
- grpclib_transports.connect_ssh(host: str, port: int = 22, *, username: str | None = None, password: str | None = None, known_hosts: Any = None, tuning: TransportTuning = TransportTuning(buffer_size=8388608, read_chunk_size=8388608, write_high_water=8388608, write_low_water=4194304, http2_stream_window_size=16777216, http2_connection_window_size=67108864, http2_max_frame_size=1048576, transfer_chunk_size=262144), status_details_codec: StatusDetailsCodecBase | None = None, **kwargs: Any) AsyncGenerator[SshChannel][source]¶
Connect to an SSH server and yield an
SshChannel.Use as an async context manager. The SSH session and channel are closed on exit.
- grpclib_transports.connect_tcp(host: str, port: int, *, tuning: TransportTuning = TransportTuning(buffer_size=8388608, read_chunk_size=8388608, write_high_water=8388608, write_low_water=4194304, http2_stream_window_size=16777216, http2_connection_window_size=67108864, http2_max_frame_size=1048576, transfer_chunk_size=262144), ssl: SSLContext | bool | None = None, **kwargs: Any) Channel[source]¶
Create a
grpclib.client.Channelto host:port over TCP.The channel is configured with tuned HTTP/2 window sizes from tuning. Pass ssl to enable TLS.
- grpclib_transports.connect_unix(path: str | Path, *, tuning: TransportTuning = TransportTuning(buffer_size=8388608, read_chunk_size=8388608, write_high_water=8388608, write_low_water=4194304, http2_stream_window_size=16777216, http2_connection_window_size=67108864, http2_max_frame_size=1048576, transfer_chunk_size=262144), **kwargs: Any) Channel[source]¶
Create a
grpclib.client.Channelto a Unix-domain socket at path.The channel is configured with tuned HTTP/2 window sizes from tuning.
- grpclib_transports.stdio_worker(argv: Sequence[str | Path], *, tuning: TransportTuning = TransportTuning(buffer_size=8388608, read_chunk_size=8388608, write_high_water=8388608, write_low_water=4194304, http2_stream_window_size=16777216, http2_connection_window_size=67108864, http2_max_frame_size=1048576, transfer_chunk_size=262144), cwd: str | Path | None = None, env: Mapping[str, str] | None = None, stderr: Any = None, status_details_codec: StatusDetailsCodecBase | None = None) AsyncGenerator[StdioChannel][source]¶
Spawn a subprocess and yield a
StdioChannelconnected to its stdin/stdout.Use as an async context manager. The subprocess is terminated on exit.
Bidirectional RPC¶
- class grpclib_transports.LogicalRpcPeer(*, send_frame: Callable[[LogicalFrame], Awaitable[None]], receive_frame: Callable[[], Awaitable[LogicalFrame | None]], handler: Callable[[str, Any], Awaitable[Any]] | None = None)[source]¶
Bases:
objectBidirectional RPC peer over a logical frame transport.
Supports request/response (
call()), one-way events (event()), and cancellation. Spawns a background reader task viastart(). Close withaclose().- Parameters:
send_frame -- Callable that sends a
LogicalFrame.receive_frame -- Callable that returns the next
LogicalFrameorNone.handler -- Optional request/event handler
(method, payload) -> result.
- class grpclib_transports.LogicalFrame(id: int, kind: Literal['request', 'response', 'event', 'cancel'], method: str | None = None, payload: Any = None, error: str | None = None)[source]¶
A logical RPC frame exchanged between peers.
- Fields:
id: Monotonic request identifier (0 for events). kind: Frame kind:
"request","response","event", or"cancel". method: gRPC-style method name for requests/events. payload: Arbitrary data carried in the frame. error: Error message carried in a response frame.
Workers¶
- class grpclib_transports.StdioPeerPool(argv: Sequence[str | Path], *, peer_factory: PeerFactory[PeerT], size: int = 1, tuning: TransportTuning = TransportTuning(buffer_size=8388608, read_chunk_size=8388608, write_high_water=8388608, write_low_water=4194304, http2_stream_window_size=16777216, http2_connection_window_size=67108864, http2_max_frame_size=1048576, transfer_chunk_size=262144), cwd: str | Path | None = None, env: Mapping[str, str] | None = None, stderr: Any = None)[source]¶
Bases:
GenericA pool of size subprocess workers, each bridged by a
LogicalRpcPeer.Use as an async context manager. On enter, spawns size child processes via
stdio_worker(), creates peers with peer_factory, and registers them inregistry. On exit, closes all peers and terminates all subprocesses.
- class grpclib_transports.PeerRegistry[source]¶
Bases:
GenericA thread-unsafe registry of
RegisteredPeerinstances.Supports
len(), iteration, and snapshot viasnapshot(). Broadcast calls to all registered peers withcall_all().
- class grpclib_transports.RegisteredPeer(id: str, peer: PeerT, metadata: Mapping[str, ~typing.Any]=<factory>)[source]¶
Bases:
GenericA
LogicalRpcPeerregistered with an ID and optional metadata.Delegates
call()andevent()to the wrapped peer.
Transfer¶
Multiprocessing¶
- class grpclib_transports.MultiprocessingPipePair(parent: MultiprocessingPipeEndpoint, child: MultiprocessingPipeEndpoint, context: Any)[source]¶
Bases:
objectA pair of
MultiprocessingPipeEndpoint— one for parent, one for child.
- class grpclib_transports.MultiprocessingPipeEndpoint(read_connection: Any, write_connection: Any, transport_name: str = 'multiprocessing')[source]¶
Bases:
objectOne end of a multiprocessing pipe pair.
Call
open_channel()to create aPipeChannelbacked by the pipe file descriptors.
- grpclib_transports.multiprocessing_pipe_pair(*, context: Any | None = None, preload: Sequence[str] = ()) MultiprocessingPipePair[source]¶
Create a
MultiprocessingPipePairfor parent-child communication.If context is not given, calls
get_forkserver_context()with preload.