client.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # Copyright 2019 the 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. """An example of cancelling requests in gRPC."""
  15. from __future__ import absolute_import
  16. from __future__ import division
  17. from __future__ import print_function
  18. import argparse
  19. import logging
  20. import signal
  21. import sys
  22. import grpc
  23. from examples.python.cancellation import hash_name_pb2
  24. from examples.python.cancellation import hash_name_pb2_grpc
  25. _DESCRIPTION = "A client for finding hashes similar to names."
  26. _LOGGER = logging.getLogger(__name__)
  27. def run_unary_client(server_target, name, ideal_distance):
  28. with grpc.insecure_channel(server_target) as channel:
  29. stub = hash_name_pb2_grpc.HashFinderStub(channel)
  30. future = stub.Find.future(hash_name_pb2.HashNameRequest(
  31. desired_name=name, ideal_hamming_distance=ideal_distance),
  32. wait_for_ready=True)
  33. def cancel_request(unused_signum, unused_frame):
  34. future.cancel()
  35. sys.exit(0)
  36. signal.signal(signal.SIGINT, cancel_request)
  37. result = future.result()
  38. print(result)
  39. def run_streaming_client(server_target, name, ideal_distance,
  40. interesting_distance):
  41. with grpc.insecure_channel(server_target) as channel:
  42. stub = hash_name_pb2_grpc.HashFinderStub(channel)
  43. result_generator = stub.FindRange(hash_name_pb2.HashNameRequest(
  44. desired_name=name,
  45. ideal_hamming_distance=ideal_distance,
  46. interesting_hamming_distance=interesting_distance),
  47. wait_for_ready=True)
  48. def cancel_request(unused_signum, unused_frame):
  49. result_generator.cancel()
  50. sys.exit(0)
  51. signal.signal(signal.SIGINT, cancel_request)
  52. for result in result_generator:
  53. print(result)
  54. def main():
  55. parser = argparse.ArgumentParser(description=_DESCRIPTION)
  56. parser.add_argument("name", type=str, help='The desired name.')
  57. parser.add_argument("--ideal-distance",
  58. default=0,
  59. nargs='?',
  60. type=int,
  61. help="The desired Hamming distance.")
  62. parser.add_argument('--server',
  63. default='localhost:50051',
  64. type=str,
  65. nargs='?',
  66. help='The host-port pair at which to reach the server.')
  67. parser.add_argument(
  68. '--show-inferior',
  69. default=None,
  70. type=int,
  71. nargs='?',
  72. help='Also show candidates with a Hamming distance less than this value.'
  73. )
  74. args = parser.parse_args()
  75. if args.show_inferior is not None:
  76. run_streaming_client(args.server, args.name, args.ideal_distance,
  77. args.show_inferior)
  78. else:
  79. run_unary_client(args.server, args.name, args.ideal_distance)
  80. if __name__ == "__main__":
  81. logging.basicConfig()
  82. main()