Registry / http-networking / grpclib

grpclib

JSON →
library0.4.9pypypi✓ verified 49d ago

grpclib is a pure-Python implementation of the gRPC protocol for asyncio, designed to give developers full control over HTTP/2 streams. It allows for building high-performance client and server applications using asynchronous Python. The current version is 0.4.9 and it requires Python 3.10 or newer. The library receives regular updates with bug fixes and new features.

http-networkingserialization
pip install "grpclib[protobuf]"
Install & Compatibility
Where this runs
tested against v0.4.9 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
9/10 runs
9/10 runs
py 3.11
9/10 runs
9/10 runs
py 3.12
9/10 runs
9/10 runs
py 3.13
9/10 runs
9/10 runs
py 3.9
9/10 runs
9/10 runs
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Channel
from grpclib.client import Channel
Server
from grpclib.server import Server
graceful_exit
from grpclib.utils import graceful_exit
Status
from grpclib import Status
GRPCError
from grpclib import GRPCError
GreeterStub
from .helloworld_grpc import GreeterStub
Example of generated client stub import (based on 'helloworld.proto')
GreeterBase
from .helloworld_grpc import GreeterBase
Example of generated server base class import (based on 'helloworld.proto')

This quickstart demonstrates how to set up a basic gRPC server and client using `grpclib`. First, define your service in a `.proto` file, then use `protoc` with the `grpclib` plugin to generate Python stubs. Finally, implement your server logic and client calls using the generated stubs.

# 1. Define your service in a .proto file (e.g., helloworld.proto): # syntax = "proto3"; # package helloworld; # message HelloRequest { string name = 1; } # message HelloReply { string message = 1; } # service Greeter { rpc SayHello (HelloRequest) returns (HelloReply); } # 2. Generate Python code from the .proto file: # python3 -m grpc_tools.protoc -I. --python_out=. --grpclib_python_out=. helloworld.proto # 3. Server implementation (server.py) import asyncio from grpclib.utils import graceful_exit from grpclib.server import Server # Generated by protoc from helloworld.proto from helloworld_pb2 import HelloReply from helloworld_grpc import GreeterBase class Greeter(GreeterBase): async def SayHello(self, stream): request = await stream.recv_message() message = f'Hello, {request.name}!' await stream.send_message(HelloReply(message=message)) async def main_server(*, host='127.0.0.1', port=50051): server = Server([Greeter()]) # Note: graceful_exit isn't supported in Windows with graceful_exit([server]): await server.start(host, port) print(f'Serving on {host}:{port}') await server.wait_closed() # 4. Client implementation (client.py) from grpclib.client import Channel # Generated by protoc from helloworld.proto from helloworld_pb2 import HelloRequest, HelloReply from helloworld_grpc import GreeterStub async def main_client(): async with Channel('127.0.0.1', 50051) as channel: greeter = GreeterStub(channel) reply = await greeter.SayHello(HelloRequest(name='Dr. Strange')) print(reply.message) # To run the server and then the client: # if __name__ == '__main__': # asyncio.run(main_server()) # # In another terminal, run: # # asyncio.run(main_client())
grpclib --version
Debug
Known issues
breakingUpgrading `grpclib` often requires updating your `protobuf` installation. `grpclib` regenerates its internal protobuf files, which can introduce new minimum `protobuf` runtime version requirements. For example, v0.4.6 required `protobuf>=3.20.0`. Always check the changelog for specific version bumps.
fix
Ensure your `protobuf` package (`pip install --upgrade protobuf`) and `protoc` compiler (`brew upgrade protobuf` or similar) are up-to-date when upgrading `grpclib`.
affects: >=0.3.x
breakingThe `protoc` plugin option `--python_grpc_out` was renamed to `--grpclib_python_out` in `grpclib` v0.3.2. Build scripts using the old option will fail.
fix
Update your `protoc` command to use `--grpclib_python_out` instead of `--python_grpc_out` for generating `grpclib` specific stubs. Example: `python3 -m grpc_tools.protoc -I. --python_out=. --grpclib_python_out=. helloworld.proto`.
affects: <0.3.2
deprecatedThe `loop` argument in public APIs (e.g., `Channel`, `Server` constructors) has been deprecated as `asyncio` automatically manages the event loop.
fix
Remove the `loop` argument from calls to `Channel`, `Server`, and other public APIs where it was previously accepted. `asyncio.run()` will handle loop management.
affects: >=0.3.x
gotchaThe `grpclib.utils.graceful_exit` utility, used for handling server shutdown signals, is not supported on Windows operating systems.
fix
On Windows, implement custom signal handling (e.g., using `asyncio.Event` and `signal.signal` for `SIGINT` on Python 3.8+ if available) or an alternative shutdown mechanism for your `grpclib` server.
affects: All versions
gotchaIn v0.4.0, metadata validation was fixed. This may cause exceptions if your application was previously sending invalid metadata values that did not conform to gRPC specifications (e.g., non-printable ASCII characters for text metadata or improperly encoded binary metadata).
fix
Ensure all metadata keys and values conform to gRPC wire format specifications: keys with `-bin` suffix for binary values (bytes type), and printable ASCII for text values (str type). `grpclib` handles base64 encoding/decoding for `-bin` suffixed values automatically.
affects: <0.4.0
breakingProtobuf message and service stubs (e.g., `_pb2.py`, `_grpclib.py`) must be generated from your `.proto` files using the `protoc` compiler and the `grpclib` plugin. If these files are missing or not in the Python path, your application will encounter a `ModuleNotFoundError`.
fix
Run the `protoc` compiler with the `grpclib` plugin to generate stubs for your `.proto` files. Example: `python3 -m grpc_tools.protoc -I. --python_out=. --grpclib_python_out=. your_service.proto`. Ensure the generated files are placed in a location discoverable by your Python application (e.g., in the same directory as your Python script or added to `PYTHONPATH`).
affects: All versions
Errors
Common errors & fixes
grpclib.exceptions.GRPCError
A gRPC call failed, either on the client or server side, due to a protocol error, invalid argument, internal server error, network issue, or explicit cancellation.
fix
On the client, catch `GRPCError` and inspect `error.status` and `error.message` to handle specific gRPC status codes. On the server, raise `GRPCError` with an appropriate `Status` enum value and an optional message to signal specific failures to the client.
StreamTerminatedError: Stream reset by remote party
The underlying HTTP/2 stream was unexpectedly terminated by the remote party (client or server), often due to a protocol violation, network issue, or a premature shutdown.
fix
Ensure both client and server applications handle stream closures gracefully; clients should call `stream.end()` for streaming calls, and servers should handle `asyncio.CancelledError` in request handlers during shutdown. Consider enabling `grpclib` debugging logs for more insights into the HTTP/2 frames.
ModuleNotFoundError: No module named 'your_service_pb2'
The Python interpreter cannot find the protobuf-generated `_pb2.py` or gRPC stub `_grpc.py` files because they are not in the Python import path, or the `protoc` command was used incorrectly during generation.
fix
Ensure `__init__.py` files exist in all directories forming the Python package structure. Generate the protobuf files using the `protoc` command with correct `-I` (proto import path) and output (`--python_out`, `--grpclib_python_out`) arguments, relative to the root of your Python package. Adjust Python import statements to reflect the correct package structure (e.g., `from . import your_service_pb2`).
AttributeError: module 'grpc' has no attribute 'aio'
This error typically occurs when attempting to use the asynchronous API of the `grpcio` library (`grpc.aio`) while `grpcio` is not installed, is an outdated version, or when `grpclib` is intended for use (which has its own distinct async API).
fix
If you intend to use `grpcio`'s async API, ensure `grpcio` is installed and updated to a version that supports `grpc.aio`. If you are using `grpclib`, use its native asynchronous components like `grpclib.client.Channel` and `grpclib.server.Server` instead of `grpc.aio` constructs.
grpclib.exceptions.ProtocolError
An underlying HTTP/2 protocol error occurred, indicating a violation of the HTTP/2 specification during communication between the gRPC client and server.
fix
This error often points to deeper issues. Review full tracebacks for details. Check for misconfigured proxies, load balancers, or firewalls that might be interfering with HTTP/2 traffic. Ensure `grpclib` versions are compatible across client and server if possible, and that both sides are handling HTTP/2 streams according to the gRPC protocol.
Upgrade
Version history
0.4.9latest on PyPI
Audit
Dependencies
protobufrequiredRequired at runtime for message serialization/deserialization.
grpcio-toolsoptionalRequired for generating gRPC stub files from .proto definitions (compile-time only).
certifioptionalOptional, for using Mozilla's collection of CA certificates for secure channels.
Agent activity
14 hits · last 30 days
node
4
seranking-bot
4
ahrefsbot
3
Amazon
1
amazonbot
1
Resources