Debian Codegraph @

examples/python/cancellation/client.py

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