• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1## Compression with gRPC Python
2
3gRPC offers lossless compression options in order to decrease the number of bits
4transferred over the wire. Three levels of compression are available:
5
6 - `grpc.Compression.NoCompression` - No compression is applied to the payload. (default)
7 - `grpc.Compression.Deflate` - The "Deflate" algorithm is applied to the payload.
8 - `grpc.Compression.Gzip` - The Gzip algorithm is applied to the payload.
9
10The default option on both clients and servers is `grpc.Compression.NoCompression`.
11
12See [the gRPC Compression Spec](https://github.com/grpc/grpc/blob/master/doc/compression.md)
13for more information.
14
15### Client Side Compression
16
17Compression may be set at two levels on the client side.
18
19#### At the channel level
20
21```python
22with grpc.insecure_channel('foo.bar:1234', compression=grpc.Compression.Gzip) as channel:
23    use_channel(channel)
24```
25
26#### At the call level
27
28Setting the compression method at the call level will override any settings on
29the channel level.
30
31```python
32stub = helloworld_pb2_grpc.GreeterStub(channel)
33response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'),
34                         compression=grpc.Compression.Deflate)
35```
36
37
38### Server Side Compression
39
40Additionally, compression may be set at two levels on the server side.
41
42#### On the entire server
43
44```python
45server = grpc.server(futures.ThreadPoolExecutor(),
46                     compression=grpc.Compression.Gzip)
47```
48
49#### For an individual RPC
50
51```python
52def SayHello(self, request, context):
53    context.set_response_compression(grpc.Compression.NoCompression)
54    return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)
55```
56
57Setting the compression method for an individual RPC will override any setting
58supplied at server creation time.
59