asyncio_route_guide_client.py
Go to the documentation of this file.
1 # Copyright 2020 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 """The Python AsyncIO implementation of the gRPC route guide client."""
15 
16 import asyncio
17 import logging
18 import random
19 from typing import Iterable, List
20 
21 import grpc
22 import route_guide_pb2
23 import route_guide_pb2_grpc
24 import route_guide_resources
25 
26 
27 def make_route_note(message: str, latitude: int,
28  longitude: int) -> route_guide_pb2.RouteNote:
30  message=message,
31  location=route_guide_pb2.Point(latitude=latitude, longitude=longitude))
32 
33 
34 # Performs an unary call
35 async def guide_get_one_feature(stub: route_guide_pb2_grpc.RouteGuideStub,
36  point: route_guide_pb2.Point) -> None:
37  feature = await stub.GetFeature(point)
38  if not feature.location:
39  print("Server returned incomplete feature")
40  return
41 
42  if feature.name:
43  print(f"Feature called {feature.name} at {feature.location}")
44  else:
45  print(f"Found no feature at {feature.location}")
46 
47 
48 async def guide_get_feature(stub: route_guide_pb2_grpc.RouteGuideStub) -> None:
50  stub, route_guide_pb2.Point(latitude=409146138, longitude=-746188906))
51  await guide_get_one_feature(stub,
52  route_guide_pb2.Point(latitude=0, longitude=0))
53 
54 
55 # Performs a server-streaming call
57  stub: route_guide_pb2_grpc.RouteGuideStub) -> None:
58  rectangle = route_guide_pb2.Rectangle(
59  lo=route_guide_pb2.Point(latitude=400000000, longitude=-750000000),
60  hi=route_guide_pb2.Point(latitude=420000000, longitude=-730000000))
61  print("Looking for features between 40, -75 and 42, -73")
62 
63  features = stub.ListFeatures(rectangle)
64 
65  async for feature in features:
66  print(f"Feature called {feature.name} at {feature.location}")
67 
68 
70  feature_list: List[route_guide_pb2.Feature]
71 ) -> Iterable[route_guide_pb2.Point]:
72  for _ in range(0, 10):
73  random_feature = random.choice(feature_list)
74  print(f"Visiting point {random_feature.location}")
75  yield random_feature.location
76 
77 
78 # Performs a client-streaming call
79 async def guide_record_route(stub: route_guide_pb2_grpc.RouteGuideStub) -> None:
81  route_iterator = generate_route(feature_list)
82 
83  # gRPC AsyncIO client-streaming RPC API accepts both synchronous iterables
84  # and async iterables.
85  route_summary = await stub.RecordRoute(route_iterator)
86  print(f"Finished trip with {route_summary.point_count} points")
87  print(f"Passed {route_summary.feature_count} features")
88  print(f"Travelled {route_summary.distance} meters")
89  print(f"It took {route_summary.elapsed_time} seconds")
90 
91 
92 def generate_messages() -> Iterable[route_guide_pb2.RouteNote]:
93  messages = [
94  make_route_note("First message", 0, 0),
95  make_route_note("Second message", 0, 1),
96  make_route_note("Third message", 1, 0),
97  make_route_note("Fourth message", 0, 0),
98  make_route_note("Fifth message", 1, 0),
99  ]
100  for msg in messages:
101  print(f"Sending {msg.message} at {msg.location}")
102  yield msg
103 
104 
105 # Performs a bidi-streaming call
106 async def guide_route_chat(stub: route_guide_pb2_grpc.RouteGuideStub) -> None:
107  # gRPC AsyncIO bidi-streaming RPC API accepts both synchronous iterables
108  # and async iterables.
109  call = stub.RouteChat(generate_messages())
110  async for response in call:
111  print(f"Received message {response.message} at {response.location}")
112 
113 
114 async def main() -> None:
115  async with grpc.aio.insecure_channel('localhost:50051') as channel:
117  print("-------------- GetFeature --------------")
118  await guide_get_feature(stub)
119  print("-------------- ListFeatures --------------")
120  await guide_list_features(stub)
121  print("-------------- RecordRoute --------------")
122  await guide_record_route(stub)
123  print("-------------- RouteChat --------------")
124  await guide_route_chat(stub)
125 
126 
127 if __name__ == '__main__':
128  logging.basicConfig(level=logging.INFO)
129  asyncio.get_event_loop().run_until_complete(main())
asyncio_route_guide_client.generate_messages
Iterable[route_guide_pb2.RouteNote] generate_messages()
Definition: asyncio_route_guide_client.py:92
capstone.range
range
Definition: third_party/bloaty/third_party/capstone/bindings/python/capstone/__init__.py:6
route_guide_pb2_grpc.RouteGuideStub
Definition: multiplex/route_guide_pb2_grpc.py:7
route_guide_resources.read_route_guide_database
def read_route_guide_database()
Definition: multiplex/route_guide_resources.py:21
asyncio_route_guide_client.make_route_note
route_guide_pb2.RouteNote make_route_note(str message, int latitude, int longitude)
Definition: asyncio_route_guide_client.py:27
asyncio_route_guide_client.main
None main()
Definition: asyncio_route_guide_client.py:114
route_guide_pb2.Point
Point
Definition: multiplex/route_guide_pb2.py:242
asyncio_route_guide_client.guide_get_feature
None guide_get_feature(route_guide_pb2_grpc.RouteGuideStub stub)
Definition: asyncio_route_guide_client.py:48
asyncio_route_guide_client.guide_record_route
None guide_record_route(route_guide_pb2_grpc.RouteGuideStub stub)
Definition: asyncio_route_guide_client.py:79
asyncio_route_guide_client.guide_list_features
None guide_list_features(route_guide_pb2_grpc.RouteGuideStub stub)
Definition: asyncio_route_guide_client.py:56
main
Definition: main.py:1
route_guide_pb2.Rectangle
Rectangle
Definition: multiplex/route_guide_pb2.py:249
asyncio_route_guide_client.guide_get_one_feature
None guide_get_one_feature(route_guide_pb2_grpc.RouteGuideStub stub, route_guide_pb2.Point point)
Definition: asyncio_route_guide_client.py:35
asyncio_route_guide_client.generate_route
Iterable[route_guide_pb2.Point] generate_route(List[route_guide_pb2.Feature] feature_list)
Definition: asyncio_route_guide_client.py:69
asyncio_route_guide_client.guide_route_chat
None guide_route_chat(route_guide_pb2_grpc.RouteGuideStub stub)
Definition: asyncio_route_guide_client.py:106
route_guide_pb2.RouteNote
RouteNote
Definition: multiplex/route_guide_pb2.py:263


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:35