gRPC Compatibility
Connect fully supports the gRPC protocol, including streaming. We validate our implementation using an extended version of Google's own interoperability tests.
Services
Running services with gRPC compatibility requires using a server that supports HTTP/2 trailers. In Python, this typically means using pyvoy, which is also what we run gRPC compatibility tests with.
If using a server with trailers support, there is no other setup required to support gRPC. If you run in a server that doesn't support it, you will get an error at runtime when making a request.
Connect-Python currently does not provide implementations of gRPC standard endpoints like reflection and health check but likely will in the future.
Clients
Clients default to using the Connect protocol. To use the gRPC protocol, set grpc = True during
client construction. If the gRPC server is using TLS, Connect clients work with no further configuration.
If the gRPC server is using HTTP/2 without TLS, you will need to additionally pass in a transport
configured to explicitly use HTTP2. Connect-Python uses pyqwest as its transport
for its support of HTTP/2 trailers and bidirectional streaming. It can be configured for HTTP/2 with
- async
- sync
async with (
HTTPTransport(http_version = HTTPVersion.HTTP2) as transport,
GreetServiceClient(url, grpc=True, http_client=Client(transport)) as client,
):
res = await client.greet(GreetRequest(name="Jane))
with (
SyncHTTPTransport(http_version = HTTPVersion.HTTP2) as transport,
GreetServiceClientSync(url, grpc=True, http_client=SyncClient(transport)) as client,
):
res = client.greet(GreetRequest(name="Jane))
Migration
There's no ironclad guide to migrating an existing connect-python service to
connect-python — every codebase uses a different subset of grpc-python
features and will need a slightly different approach. Unlike many RPC framework
migrations, remember that you do not need to modify your service's clients:
they can continue to use their current gRPC clients. Your current Protobuf
schema will also work without modification.
The particulars of your codebase will be unique, but most migrations include a few common steps:
- Begin generating code with
protoc-gen-connect-python. During the migration, your code can importconnect-pythonandgrpc-pythoncode without any problems. - Migrate service implementations to Connect generated stubs. It's recommended to extend the protocol classes
to have type checking find differences in method names. Change uses of
abortto directlyraise ConnectError- for Connect services, it's uncommon to pass theRequestContextinto business logic code. - Once you've migrated your service implementations to Connect, replace your
mainfunction which starts a gRPC server with module-level initialization of anappor similarly named variable set to the WSGI or ASGI application corresponding to your service. For ASGI applications needing async initailization, pass anasync deffunction that yields your initialized service to the application constructor. - Setup your local development and production scripts such as Dockerfile to run the application with pyvoy
and release. You now have a server handling requests with
connect-pythonthat supports both Connect and gRPC clients. - Next migrate clients to use Connect. Replace any special configuration of
ManagedChannelwith configuredpyqwest.HTTPTransportorpyqwest.SyncHTTPTransportand switch to Connect's generated client types. If passingmetadataat the call site, change toheaders- lists of string tuples can be passed directly to aHeadersconstructor, or can be changed to a raw dictionary. Update any error handling to catchConnectError. - You're done! When all your services have been deployed, you can stop generating code with gRPC.