Is it possible to enable GRPC message compression

2019-08-27 19:42发布

问题:

I have a gRPC client (in Java) sending requests to a server (written in Python). I need to enable both request compression and response compression. There is good documentation on how to enable compression on the client side. I have managed to compress the request like so:

 response = blockingStub.withCompression("gzip").method(request)

However, I cannot find any documentation on how to compress the server response (also). It seems that there is almost no documentation (or examples) on how to use gRPC message compression in Python. How can I enable server-side compression? The call is a simple RPC call (no streaming).

回答1:

Compression in Python is supported via metadata arguments.

You can find an example here: https://github.com/grpc/grpc/blob/master/src/python/grpcio_tests/tests/unit/_compression_test.py



回答2:

To add to kpayson64's answer which covers call-level compression, you can set compression settings on a grpc.Channel or grpc.Server object upon initialization via the channel arg grpc.default_compression_algorithm.

Please also see the gRPC Compression Cookbook on recommendations on where to or not to explicitly enable compression.