Files
grpc/examples/python/compression
Sergii Tkachenko 0497da859e [CI] Use python 3.9+ everywhere (#40139)
- Upgrade the base image of  https://us-docker.pkg.dev/grpc-testing/testing-images-public/bazel to newer https://gcr.io/oss-fuzz-base/base-builder@sha256:4f3ca10accd14292556601d70e457fa85ad57180c913484427869d3379a07684, which comes with python3.11

  Before:
  ```

  Python 3.8.3 (default, Mar 12 2024, 03:21:22)
  [Clang 15.0.0 (https://github.com/llvm/llvm-project.git bf7f8d6fa6f460bf0a16ffe
  ```

  After:
  ```
  Python 3.11.13 (main, Jul 16 2025, 03:42:11)
  [Clang 18.1.8 (https://github.com/llvm/llvm-project.git 3b5b5c1ec4a3095ab096dd78
  ```
- Refactor and improve  https://us-docker.pkg.dev/grpc-testing/testing-images-public/sanity. Removed building python 3.7 from source.
- Switch Windows builds to use `3.9.13` (previously `3.8.10`)
- Changed minimum python version in bazel builds from 3.8 to 3.9.
- Updated pylint to use python3.11.
- Various minor compatibility fixes.

There's a few other dependencies/settings that can be upgraded now that we're using 3.9+. I'll make them in a follow-up PR.

Closes #40139

PiperOrigin-RevId: 786775922
2025-07-24 11:31:19 -07:00
..

Compression with gRPC Python

gRPC offers lossless compression options in order to decrease the number of bits transferred over the wire. Three levels of compression are available:

  • grpc.Compression.NoCompression - No compression is applied to the payload. (default)
  • grpc.Compression.Deflate - The "Deflate" algorithm is applied to the payload.
  • grpc.Compression.Gzip - The Gzip algorithm is applied to the payload.

The default option on both clients and servers is grpc.Compression.NoCompression.

See the gRPC Compression Spec for more information.

Client Side Compression

Compression may be set at two levels on the client side.

At the channel level

with grpc.insecure_channel('foo.bar:1234', compression=grpc.Compression.Gzip) as channel:
    use_channel(channel)

At the call level

Setting the compression method at the call level will override any settings on the channel level.

stub = helloworld_pb2_grpc.GreeterStub(channel)
response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'),
                         compression=grpc.Compression.Deflate)

Server Side Compression

Additionally, compression may be set at two levels on the server side.

On the entire server

server = grpc.server(futures.ThreadPoolExecutor(),
                     compression=grpc.Compression.Gzip)

For an individual RPC

def SayHello(self, request, context):
    context.set_compression(grpc.Compression.NoCompression)
    return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)

Setting the compression method for an individual RPC will override any setting supplied at server creation time.