API Reference¶
Protocol & Tuning¶
- class grpclib_transports.TransportTuning(buffer_size, read_chunk_size, write_high_water, write_low_water, http2_stream_window_size, http2_connection_window_size, http2_max_frame_size, transfer_chunk_size)[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.
- Parameters:
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)
- 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, username, uid=None, gid=None, pid=None, group_ids=None, group_names=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).
- Parameters:
transport (str)
username (str | None)
uid (int | None)
gid (int | None)
pid (int | None)
group_ids (tuple[int, ...] | None)
group_names (tuple[str, ...] | None)
- grpclib_transports.local_process_identity(*, transport)[source]¶
Build a
PeerIdentityfor the current process.- Parameters:
transport (str)
- Return type:
- grpclib_transports.peer_identity_from_transport(transport)[source]¶
Extract
PeerIdentityfrom an asyncio transport.Checks for a stored
peer_identityextra, an SSH username, or a Unix-domain socket withSO_PEERCRED.- Parameters:
transport (Any)
- Return type:
- grpclib_transports.peer_identity_from_stream(stream)[source]¶
Extract
PeerIdentityfrom a gRPC stream's underlying transport.- Parameters:
stream (Any)
- Return type:
- grpclib_transports.make_h2_config(*, client_side)[source]¶
Build an
h2configuration with strict validation off.Disables inbound/outbound header validation and normalization so protobuf-based headers pass through unchanged.
- Parameters:
client_side (bool)
- Return type:
H2Configuration
- grpclib_transports.make_config(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))[source]¶
Build a grpclib
Configurationwith tuned window sizes.- Parameters:
tuning (TransportTuning)
- Return type:
Configuration
- grpclib_transports.make_server_protocol(mapping, *, 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), status_details_codec=None)[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.- Parameters:
mapping (dict[str, Handler])
tuning (TransportTuning)
status_details_codec (StatusDetailsCodecBase | None)
- Return type:
H2Protocol
- grpclib_transports.init_h2_transport(protocol, transport, *, 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))[source]¶
Wire an H2 protocol to a transport and advertise a tuned max frame size.
- Parameters:
protocol (H2Protocol)
transport (Any)
tuning (TransportTuning)
- Return type:
None
- grpclib_transports.build_mapping(handlers)[source]¶
Merge
__mapping__()from a sequence of servable handlers into one dict.- Parameters:
handlers (Sequence[IServable])
- Return type:
dict[str, Handler]
- async grpclib_transports.pump(protocol, reader, *, 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))[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().- Parameters:
protocol (H2Protocol)
reader (Any)
tuning (TransportTuning)
- Return type:
None
- async grpclib_transports.serve_h2(handlers, reader, transport, *, 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), max_concurrency=None, status_details_codec=None)[source]¶
Build a server protocol, wire it to a transport, and pump frames.
- Parameters:
handlers (Sequence[IServable])
reader (Any)
transport (Any)
tuning (TransportTuning)
max_concurrency (int | None)
status_details_codec (StatusDetailsCodecBase | None)
- Return type:
None
- grpclib_transports.pause_h2_protocol(protocol)[source]¶
Pause writing on an H2 protocol, if set.
- Parameters:
protocol (BaseProtocol | None)
- Return type:
None
- grpclib_transports.resume_h2_protocol(protocol)[source]¶
Resume writing on an H2 protocol, if set.
- Parameters:
protocol (BaseProtocol | None)
- Return type:
None
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.
- set_protocol(protocol)[source]¶
Set a new protocol.
- Parameters:
protocol (BaseProtocol)
- Return type:
None
- class grpclib_transports.StdioTransport(reader, writer, *, 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))[source]¶
Bases:
BaseCustomTransportAn asyncio transport that wraps a stdio subprocess pipe pair.
- Parameters:
reader (asyncio.StreamReader)
writer (asyncio.StreamWriter)
tuning (TransportTuning)
- write(data)[source]¶
Write some data bytes to the transport.
This does not block; it buffers the data and arranges for it to be sent out asynchronously.
- Parameters:
data (bytes | bytearray | memoryview)
- Return type:
None
- close()[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.
- Return type:
None
- get_extra_info(name, default=None)[source]¶
Get optional transport information.
- Parameters:
name (str)
default (Any)
- Return type:
Any
- abort()[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.
- Return type:
None
- class grpclib_transports.SshTransport(reader, writer, *, 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))[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.- Parameters:
reader (Any)
writer (Any)
tuning (TransportTuning)
- write(data)[source]¶
Write some data bytes to the transport.
This does not block; it buffers the data and arranges for it to be sent out asynchronously.
- Parameters:
data (bytes | bytearray | memoryview)
- Return type:
None
- close()[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.
- Return type:
None
- get_extra_info(name, default=None)[source]¶
Get optional transport information.
- Parameters:
name (str)
default (Any)
- Return type:
Any
- abort()[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.
- Return type:
None
- can_write_eof()[source]¶
Return True if this transport supports write_eof(), False if not.
- Return type:
bool
- write_eof()[source]¶
Close the write end after flushing buffered data.
(This is like typing ^D into a UNIX program reading from stdin.)
Data may still be received.
- Return type:
None
- set_write_buffer_limits(high=None, low=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.
- Parameters:
high (int | None)
low (int | None)
- Return type:
None
- class grpclib_transports.PipeTransport(reader, writer, *, transport_name='pipe', 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))[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.- Parameters:
reader (asyncio.StreamReader)
writer (asyncio.StreamWriter)
transport_name (str)
tuning (TransportTuning)
- write(data)[source]¶
Write some data bytes to the transport.
This does not block; it buffers the data and arranges for it to be sent out asynchronously.
- Parameters:
data (bytes | bytearray | memoryview)
- Return type:
None
- close()[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.
- Return type:
None
- get_extra_info(name, default=None)[source]¶
Get optional transport information.
- Parameters:
name (str)
default (Any)
- Return type:
Any
- abort()[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.
- Return type:
None
Channels¶
- class grpclib_transports.StdioChannel(reader, writer, *, transport=None, 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), **kwargs)[source]¶
Bases:
ChannelA gRPC channel that speaks H2 over a subprocess stdio pipe pair.
- Parameters:
reader (Any)
writer (Any)
transport (StdioTransport | None)
tuning (TransportTuning)
kwargs (Any)
- class grpclib_transports.SshChannel(reader, writer, *, 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), **kwargs)[source]¶
Bases:
ChannelA gRPC channel that speaks H2 over an asyncssh session.
- Parameters:
reader (Any)
writer (Any)
tuning (TransportTuning)
kwargs (Any)
- class grpclib_transports.PipeChannel(reader, writer, *, transport=None, 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), **kwargs)[source]¶
Bases:
ChannelA gRPC channel that speaks H2 over a raw OS pipe pair.
- Parameters:
reader (Any)
writer (Any)
transport (PipeTransport | None)
tuning (TransportTuning)
kwargs (Any)
Server¶
- class grpclib_transports.Server(*, 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))[source]¶
Bases:
objectContainer for multiple service endpoints and worker managers.
- Parameters:
tuning (TransportTuning)
- async grpclib_transports.serve_stdio(handlers, *, 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), max_concurrency=None, status_details_codec=None)[source]¶
Serve gRPC over the current process's stdin/stdout.
take_wire_descriptors()moves the pipe pair off descriptors 0 and 1 first, so that only H2 frames go over the wire. The redirection below adds the one case that cannot reach: a caller that replacedsys.stdoutwith an object of its own, which writes wherever that object writes.- Parameters:
handlers (list[IServable])
tuning (TransportTuning)
max_concurrency (int | None)
status_details_codec (StatusDetailsCodecBase | None)
- Return type:
None
- async grpclib_transports.serve_ssh(handlers, host='127.0.0.1', port=8022, *, 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), max_concurrency=None, status_details_codec=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.
- Parameters:
handlers (list[IServable])
host (str)
port (int)
tuning (TransportTuning)
max_concurrency (int | None)
status_details_codec (StatusDetailsCodecBase | None)
- Return type:
None
Client¶
- grpclib_transports.connect_ssh(host, port=22, *, username=None, password=None, known_hosts=None, 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), status_details_codec=None, **kwargs)[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.
- Parameters:
host (str)
port (int)
username (str | None)
password (str | None)
known_hosts (Any)
tuning (TransportTuning)
status_details_codec (StatusDetailsCodecBase | None)
kwargs (Any)
- Return type:
AsyncGenerator[SshChannel]
- grpclib_transports.connect_tcp(host, port, *, 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), ssl=None, **kwargs)[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.
- Parameters:
host (str)
port (int)
tuning (TransportTuning)
ssl (SSLContext | bool | None)
kwargs (Any)
- Return type:
Channel
- grpclib_transports.connect_unix(path, *, 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), **kwargs)[source]¶
Create a
grpclib.client.Channelto a Unix-domain socket at path.The channel is configured with tuned HTTP/2 window sizes from tuning.
- Parameters:
path (str | Path)
tuning (TransportTuning)
kwargs (Any)
- Return type:
Channel
- grpclib_transports.stdio_worker(argv, *, 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), cwd=None, env=None, stderr=None, status_details_codec=None, on_process_start=None)[source]¶
Spawn a subprocess and yield a
StdioChannelconnected to its stdin/stdout.Use as an async context manager. The subprocess is terminated on exit.
on_process_startreceives the process as soon as it exists, and is the only way a caller reaches it: the channel yielded below carries the wire and nothing about the peer.multiprocessing_workertakes the same hook for the same reason. A caller needs it to read the exit status, which is what tells an abort from an ordinary exit, and to act on the pid.- Parameters:
argv (Sequence[str | Path])
tuning (TransportTuning)
cwd (str | Path | None)
env (Mapping[str, str] | None)
stderr (Any)
status_details_codec (StatusDetailsCodecBase | None)
on_process_start (Callable[[asyncio.subprocess.Process], None] | None)
- Return type:
AsyncGenerator[StdioChannel]
Bidirectional RPC¶
- class grpclib_transports.LogicalRpcPeer(*, send_frame, receive_frame, handler=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 (FrameSender) -- Callable that sends a
LogicalFrame.receive_frame (FrameReceiver) -- Callable that returns the next
LogicalFrameorNone.handler (RequestHandler | None) -- Optional request/event handler
(method, payload) -> result.
- class grpclib_transports.LogicalFrame(id, kind, method=None, payload=None, error=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.
- Parameters:
id (int)
kind (Literal['request', 'response', 'event', 'cancel'])
method (str | None)
payload (Any)
error (str | None)
Workers¶
- class grpclib_transports.StdioPeerPool(argv, *, peer_factory, size=1, 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), cwd=None, env=None, stderr=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.- Parameters:
argv (Sequence[str | Path])
peer_factory (PeerFactory[PeerT])
size (int)
tuning (TransportTuning)
cwd (str | Path | None)
env (Mapping[str, str] | None)
stderr (Any)
- 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, peer, metadata=<factory>)[source]¶
Bases:
GenericA
LogicalRpcPeerregistered with an ID and optional metadata.Delegates
call()andevent()to the wrapped peer.- Parameters:
id (str)
peer (PeerT)
metadata (Mapping[str, Any])
Transfer¶
Multiprocessing¶
- class grpclib_transports.MultiprocessingPipePair(parent, child, context)[source]¶
Bases:
objectA pair of
MultiprocessingPipeEndpoint— one for parent, one for child.- Parameters:
parent (MultiprocessingPipeEndpoint)
child (MultiprocessingPipeEndpoint)
context (Any)
- class grpclib_transports.MultiprocessingPipeEndpoint(read_connection, write_connection, transport_name='multiprocessing')[source]¶
Bases:
objectOne end of a multiprocessing pipe pair.
Call
open_channel()to create aPipeChannelbacked by the pipe file descriptors.- Parameters:
read_connection (Any)
write_connection (Any)
transport_name (str)
- grpclib_transports.multiprocessing_pipe_pair(*, context=None, preload=())[source]¶
Create a
MultiprocessingPipePairfor parent-child communication.If context is not given, calls
get_worker_context()with preload.- Parameters:
context (Any | None)
preload (Sequence[str])
- Return type:
- grpclib_transports.get_worker_context(method='forkserver', *, preload=())[source]¶
Return a
multiprocessingcontext for method, optionally preloading modules.preloadapplies toforkserveralone. It is what makes that method cheap: the forkserver imports the list once and each worker is a fork of that. No other start method has an equivalent, and asking aspawncontext for one raisesAttributeError.Measured, 5 workers each, preloading one module that imports Nix:
forkserver 7.7 ms per worker spawn 72.1 ms per worker
``spawn`` is the one that works in a forked process.
multiprocessing.forkserver.ForkServercarries no pid guard, so a child that inherits a running forkserver reachesos.waitpid(self._forkserver_pid, WNOHANG)on a process that is not its own child, andensure_runningraisesChildProcessError. Measured, in a forked child:spawn -> start() succeeded forkserver -> ChildProcessError: [Errno 10] No child processes
- Parameters:
method (str)
preload (Sequence[str])
- Return type:
Any