diff --git a/examples/python/Makefile b/examples/python/Makefile index 323e6b6bfb3..f0f478d8368 100644 --- a/examples/python/Makefile +++ b/examples/python/Makefile @@ -80,6 +80,10 @@ ARTIFACTS += lb_policies/helloworld_pb2.py ARTIFACTS += lb_policies/helloworld_pb2_grpc.py ARTIFACTS += lb_policies/helloworld_pb2.pyi +ARTIFACTS += compression/helloworld_pb2.py +ARTIFACTS += compression/helloworld_pb2_grpc.py +ARTIFACTS += compression/helloworld_pb2.pyi + .PHONY: all all: ${ARTIFACTS} @@ -132,6 +136,9 @@ data_transmission/demo_pb2.py data_transmission/demo_pb2_grpc.py data_transmissi lb_policies/helloworld_pb2.py lb_policies/helloworld_pb2_grpc.py lb_policies/helloworld_pb2.pyi: ../protos/helloworld.proto python3 -m grpc_tools.protoc --python_out=lb_policies --grpc_python_out=lb_policies --pyi_out=lb_policies -I ../protos ../protos/helloworld.proto +compression/helloworld_pb2.py compression/helloworld_pb2_grpc.py compression/helloworld_pb2.pyi: ../protos/helloworld.proto + python3 -m grpc_tools.protoc --python_out=compression --grpc_python_out=compression --pyi_out=compression -I ../protos ../protos/helloworld.proto + .PHONY: clean clean: rm -f ${ARTIFACTS} diff --git a/examples/python/compression/README.md b/examples/python/compression/README.md index c719bba07f8..1aa5107039a 100644 --- a/examples/python/compression/README.md +++ b/examples/python/compression/README.md @@ -50,7 +50,7 @@ server = grpc.server(futures.ThreadPoolExecutor(), ```python def SayHello(self, request, context): - context.set_response_compression(grpc.Compression.NoCompression) + context.set_compression(grpc.Compression.NoCompression) return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name) ``` diff --git a/examples/python/compression/client.py b/examples/python/compression/client.py index 89ea855bdc0..166739f7e10 100644 --- a/examples/python/compression/client.py +++ b/examples/python/compression/client.py @@ -21,9 +21,8 @@ import argparse import logging import grpc - -from examples.protos import helloworld_pb2 -from examples.protos import helloworld_pb2_grpc +import helloworld_pb2 +import helloworld_pb2_grpc _DESCRIPTION = "A client capable of compression." _COMPRESSION_OPTIONS = { @@ -33,6 +32,7 @@ _COMPRESSION_OPTIONS = { } _LOGGER = logging.getLogger(__name__) +_RPC_COUNT = 10 def run_client(channel_compression, call_compression, target): @@ -40,12 +40,13 @@ def run_client(channel_compression, call_compression, target): target, compression=channel_compression ) as channel: stub = helloworld_pb2_grpc.GreeterStub(channel) - response = stub.SayHello( - helloworld_pb2.HelloRequest(name="you"), - compression=call_compression, - wait_for_ready=True, - ) - print("Response: {}".format(response)) + for _ in range(_RPC_COUNT): + response = stub.SayHello( + helloworld_pb2.HelloRequest(name="you"), + compression=call_compression, + wait_for_ready=True, + ) + print("Response: {}".format(response)) def main(): diff --git a/examples/python/compression/helloworld_pb2.py b/examples/python/compression/helloworld_pb2.py new file mode 100644 index 00000000000..f5b4f2d27dc --- /dev/null +++ b/examples/python/compression/helloworld_pb2.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +# Generated by the protocol buffer compiler. DO NOT EDIT! +# source: helloworld.proto +"""Generated protocol buffer code.""" +from google.protobuf.internal import builder as _builder +from google.protobuf import descriptor as _descriptor +from google.protobuf import descriptor_pool as _descriptor_pool +from google.protobuf import symbol_database as _symbol_database +# @@protoc_insertion_point(imports) + +_sym_db = _symbol_database.Default() + + + + +DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x10helloworld.proto\x12\nhelloworld\"\x1c\n\x0cHelloRequest\x12\x0c\n\x04name\x18\x01 \x01(\t\"\x1d\n\nHelloReply\x12\x0f\n\x07message\x18\x01 \x01(\t2I\n\x07Greeter\x12>\n\x08SayHello\x12\x18.helloworld.HelloRequest\x1a\x16.helloworld.HelloReply\"\x00\x42\x36\n\x1bio.grpc.examples.helloworldB\x0fHelloWorldProtoP\x01\xa2\x02\x03HLWb\x06proto3') + +_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, globals()) +_builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'helloworld_pb2', globals()) +if _descriptor._USE_C_DESCRIPTORS == False: + + DESCRIPTOR._options = None + DESCRIPTOR._serialized_options = b'\n\033io.grpc.examples.helloworldB\017HelloWorldProtoP\001\242\002\003HLW' + _HELLOREQUEST._serialized_start=32 + _HELLOREQUEST._serialized_end=60 + _HELLOREPLY._serialized_start=62 + _HELLOREPLY._serialized_end=91 + _GREETER._serialized_start=93 + _GREETER._serialized_end=166 +# @@protoc_insertion_point(module_scope) diff --git a/examples/python/compression/helloworld_pb2.pyi b/examples/python/compression/helloworld_pb2.pyi new file mode 100644 index 00000000000..8c4b5b22805 --- /dev/null +++ b/examples/python/compression/helloworld_pb2.pyi @@ -0,0 +1,17 @@ +from google.protobuf import descriptor as _descriptor +from google.protobuf import message as _message +from typing import ClassVar as _ClassVar, Optional as _Optional + +DESCRIPTOR: _descriptor.FileDescriptor + +class HelloReply(_message.Message): + __slots__ = ["message"] + MESSAGE_FIELD_NUMBER: _ClassVar[int] + message: str + def __init__(self, message: _Optional[str] = ...) -> None: ... + +class HelloRequest(_message.Message): + __slots__ = ["name"] + NAME_FIELD_NUMBER: _ClassVar[int] + name: str + def __init__(self, name: _Optional[str] = ...) -> None: ... diff --git a/examples/python/compression/helloworld_pb2_grpc.py b/examples/python/compression/helloworld_pb2_grpc.py new file mode 100644 index 00000000000..47c186976e1 --- /dev/null +++ b/examples/python/compression/helloworld_pb2_grpc.py @@ -0,0 +1,70 @@ +# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT! +"""Client and server classes corresponding to protobuf-defined services.""" +import grpc + +import helloworld_pb2 as helloworld__pb2 + + +class GreeterStub(object): + """The greeting service definition. + """ + + def __init__(self, channel): + """Constructor. + + Args: + channel: A grpc.Channel. + """ + self.SayHello = channel.unary_unary( + '/helloworld.Greeter/SayHello', + request_serializer=helloworld__pb2.HelloRequest.SerializeToString, + response_deserializer=helloworld__pb2.HelloReply.FromString, + ) + + +class GreeterServicer(object): + """The greeting service definition. + """ + + def SayHello(self, request, context): + """Sends a greeting + """ + context.set_code(grpc.StatusCode.UNIMPLEMENTED) + context.set_details('Method not implemented!') + raise NotImplementedError('Method not implemented!') + + +def add_GreeterServicer_to_server(servicer, server): + rpc_method_handlers = { + 'SayHello': grpc.unary_unary_rpc_method_handler( + servicer.SayHello, + request_deserializer=helloworld__pb2.HelloRequest.FromString, + response_serializer=helloworld__pb2.HelloReply.SerializeToString, + ), + } + generic_handler = grpc.method_handlers_generic_handler( + 'helloworld.Greeter', rpc_method_handlers) + server.add_generic_rpc_handlers((generic_handler,)) + + + # This class is part of an EXPERIMENTAL API. +class Greeter(object): + """The greeting service definition. + """ + + @staticmethod + def SayHello(request, + target, + options=(), + channel_credentials=None, + call_credentials=None, + insecure=False, + compression=None, + wait_for_ready=None, + timeout=None, + metadata=None): + return grpc.experimental.unary_unary(request, target, '/helloworld.Greeter/SayHello', + helloworld__pb2.HelloRequest.SerializeToString, + helloworld__pb2.HelloReply.FromString, + options, channel_credentials, + insecure, call_credentials, compression, wait_for_ready, timeout, metadata) diff --git a/examples/python/compression/server.py b/examples/python/compression/server.py index d7da982e2a0..2661481475b 100644 --- a/examples/python/compression/server.py +++ b/examples/python/compression/server.py @@ -23,9 +23,8 @@ import logging import threading import grpc - -from examples.protos import helloworld_pb2 -from examples.protos import helloworld_pb2_grpc +import helloworld_pb2 +import helloworld_pb2_grpc _DESCRIPTION = "A server capable of compression." _COMPRESSION_OPTIONS = { @@ -41,7 +40,7 @@ _SERVER_HOST = "localhost" class Greeter(helloworld_pb2_grpc.GreeterServicer): def __init__(self, no_compress_every_n): super(Greeter, self).__init__() - self._no_compress_every_n = 0 + self._no_compress_every_n = no_compress_every_n self._request_counter = 0 self._counter_lock = threading.RLock() @@ -58,7 +57,7 @@ class Greeter(helloworld_pb2_grpc.GreeterServicer): def SayHello(self, request, context): if self._should_suppress_compression(): - context.set_response_compression(grpc.Compression.NoCompression) + context.set_compression(grpc.Compression.NoCompression) return helloworld_pb2.HelloReply(message="Hello, %s!" % request.name) diff --git a/examples/python/compression/test/compression_example_test.py b/examples/python/compression/test/compression_example_test.py index 27af6ef9293..7ddfa6d1ca9 100644 --- a/examples/python/compression/test/compression_example_test.py +++ b/examples/python/compression/test/compression_example_test.py @@ -49,6 +49,8 @@ class CompressionExampleTest(unittest.TestCase): str(test_port), "--server_compression", "gzip", + "--no_compress_every_n", + "3", ) ) try: