sync_server.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # Copyright 2019 gRPC authors.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import argparse
  15. from concurrent import futures
  16. from time import sleep
  17. import grpc
  18. from src.proto.grpc.testing import messages_pb2
  19. from src.proto.grpc.testing import test_pb2_grpc
  20. from tests.unit.framework.common import test_constants
  21. # TODO (https://github.com/grpc/grpc/issues/19762)
  22. # Change for an asynchronous server version once it's implemented.
  23. class TestServiceServicer(test_pb2_grpc.TestServiceServicer):
  24. def UnaryCall(self, request, context):
  25. return messages_pb2.SimpleResponse()
  26. def EmptyCall(self, request, context):
  27. while True:
  28. sleep(test_constants.LONG_TIMEOUT)
  29. if __name__ == "__main__":
  30. parser = argparse.ArgumentParser(description='Synchronous gRPC server.')
  31. parser.add_argument(
  32. '--host_and_port',
  33. required=True,
  34. type=str,
  35. nargs=1,
  36. help='the host and port to listen.')
  37. args = parser.parse_args()
  38. server = grpc.server(
  39. futures.ThreadPoolExecutor(max_workers=1),
  40. options=(('grpc.so_reuseport', 1),))
  41. test_pb2_grpc.add_TestServiceServicer_to_server(TestServiceServicer(),
  42. server)
  43. server.add_insecure_port(args.host_and_port[0])
  44. server.start()
  45. server.wait_for_termination()