aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/python/route_guide
diff options
context:
space:
mode:
Diffstat (limited to 'examples/python/route_guide')
-rw-r--r--examples/python/route_guide/route_guide_client.py26
-rw-r--r--examples/python/route_guide/route_guide_pb2.py172
-rw-r--r--[-rwxr-xr-x]examples/python/route_guide/route_guide_resources.py0
-rw-r--r--examples/python/route_guide/route_guide_server.py7
-rw-r--r--[-rwxr-xr-x]examples/python/route_guide/run_codegen.py (renamed from examples/python/route_guide/run_codegen.sh)16
5 files changed, 164 insertions, 57 deletions
diff --git a/examples/python/route_guide/route_guide_client.py b/examples/python/route_guide/route_guide_client.py
index ffcbd061d6..8a80ed892d 100644
--- a/examples/python/route_guide/route_guide_client.py
+++ b/examples/python/route_guide/route_guide_client.py
@@ -34,13 +34,11 @@ from __future__ import print_function
import random
import time
-from grpc.beta import implementations
+import grpc
import route_guide_pb2
import route_guide_resources
-_TIMEOUT_SECONDS = 30
-
def make_route_note(message, latitude, longitude):
return route_guide_pb2.RouteNote(
@@ -49,7 +47,7 @@ def make_route_note(message, latitude, longitude):
def guide_get_one_feature(stub, point):
- feature = stub.GetFeature(point, _TIMEOUT_SECONDS)
+ feature = stub.GetFeature(point)
if not feature.location:
print("Server returned incomplete feature")
return
@@ -66,14 +64,12 @@ def guide_get_feature(stub):
def guide_list_features(stub):
- rect = route_guide_pb2.Rectangle(
- lo=route_guide_pb2.Point(
- latitude=400000000, longitude = -750000000),
- hi=route_guide_pb2.Point(
- latitude = 420000000, longitude = -730000000))
+ rectangle = route_guide_pb2.Rectangle(
+ lo=route_guide_pb2.Point(latitude=400000000, longitude=-750000000),
+ hi=route_guide_pb2.Point(latitude=420000000, longitude=-730000000))
print("Looking for features between 40, -75 and 42, -73")
- features = stub.ListFeatures(rect, _TIMEOUT_SECONDS)
+ features = stub.ListFeatures(rectangle)
for feature in features:
print("Feature called %s at %s" % (feature.name, feature.location))
@@ -90,8 +86,8 @@ def generate_route(feature_list):
def guide_record_route(stub):
feature_list = route_guide_resources.read_route_guide_database()
- route_iter = generate_route(feature_list)
- route_summary = stub.RecordRoute(route_iter, _TIMEOUT_SECONDS)
+ route_iterator = generate_route(feature_list)
+ route_summary = stub.RecordRoute(route_iterator)
print("Finished trip with %s points " % route_summary.point_count)
print("Passed %s features " % route_summary.feature_count)
print("Travelled %s meters " % route_summary.distance)
@@ -113,14 +109,14 @@ def generate_messages():
def guide_route_chat(stub):
- responses = stub.RouteChat(generate_messages(), _TIMEOUT_SECONDS)
+ responses = stub.RouteChat(generate_messages())
for response in responses:
print("Received message %s at %s" % (response.message, response.location))
def run():
- channel = implementations.insecure_channel('localhost', 50051)
- stub = route_guide_pb2.beta_create_RouteGuide_stub(channel)
+ channel = grpc.insecure_channel('localhost:50051')
+ stub = route_guide_pb2.RouteGuideStub(channel)
print("-------------- GetFeature --------------")
guide_get_feature(stub)
print("-------------- ListFeatures --------------")
diff --git a/examples/python/route_guide/route_guide_pb2.py b/examples/python/route_guide/route_guide_pb2.py
index 81d5d07527..924e186e06 100644
--- a/examples/python/route_guide/route_guide_pb2.py
+++ b/examples/python/route_guide/route_guide_pb2.py
@@ -277,13 +277,122 @@ _sym_db.RegisterMessage(RouteSummary)
DESCRIPTOR.has_options = True
DESCRIPTOR._options = _descriptor._ParseOptions(descriptor_pb2.FileOptions(), _b('\n\033io.grpc.examples.routeguideB\017RouteGuideProtoP\001\242\002\003RTG'))
-import abc
-import six
+import grpc
from grpc.beta import implementations as beta_implementations
from grpc.beta import interfaces as beta_interfaces
from grpc.framework.common import cardinality
from grpc.framework.interfaces.face import utilities as face_utilities
+
+class RouteGuideStub(object):
+ """Interface exported by the server.
+ """
+
+ def __init__(self, channel):
+ """Constructor.
+
+ Args:
+ channel: A grpc.Channel.
+ """
+ self.GetFeature = channel.unary_unary(
+ '/routeguide.RouteGuide/GetFeature',
+ request_serializer=Point.SerializeToString,
+ response_deserializer=Feature.FromString,
+ )
+ self.ListFeatures = channel.unary_stream(
+ '/routeguide.RouteGuide/ListFeatures',
+ request_serializer=Rectangle.SerializeToString,
+ response_deserializer=Feature.FromString,
+ )
+ self.RecordRoute = channel.stream_unary(
+ '/routeguide.RouteGuide/RecordRoute',
+ request_serializer=Point.SerializeToString,
+ response_deserializer=RouteSummary.FromString,
+ )
+ self.RouteChat = channel.stream_stream(
+ '/routeguide.RouteGuide/RouteChat',
+ request_serializer=RouteNote.SerializeToString,
+ response_deserializer=RouteNote.FromString,
+ )
+
+
+class RouteGuideServicer(object):
+ """Interface exported by the server.
+ """
+
+ def GetFeature(self, request, context):
+ """A simple RPC.
+
+ Obtains the feature at a given position.
+
+ A feature with an empty name is returned if there's no feature at the given
+ position.
+ """
+ context.set_code(grpc.StatusCode.UNIMPLEMENTED)
+ context.set_details('Method not implemented!')
+ raise NotImplementedError('Method not implemented!')
+
+ def ListFeatures(self, request, context):
+ """A server-to-client streaming RPC.
+
+ Obtains the Features available within the given Rectangle. Results are
+ streamed rather than returned at once (e.g. in a response message with a
+ repeated field), as the rectangle may cover a large area and contain a
+ huge number of features.
+ """
+ context.set_code(grpc.StatusCode.UNIMPLEMENTED)
+ context.set_details('Method not implemented!')
+ raise NotImplementedError('Method not implemented!')
+
+ def RecordRoute(self, request_iterator, context):
+ """A client-to-server streaming RPC.
+
+ Accepts a stream of Points on a route being traversed, returning a
+ RouteSummary when traversal is completed.
+ """
+ context.set_code(grpc.StatusCode.UNIMPLEMENTED)
+ context.set_details('Method not implemented!')
+ raise NotImplementedError('Method not implemented!')
+
+ def RouteChat(self, request_iterator, context):
+ """A Bidirectional streaming RPC.
+
+ Accepts a stream of RouteNotes sent while a route is being traversed,
+ while receiving other RouteNotes (e.g. from other users).
+ """
+ context.set_code(grpc.StatusCode.UNIMPLEMENTED)
+ context.set_details('Method not implemented!')
+ raise NotImplementedError('Method not implemented!')
+
+
+def add_RouteGuideServicer_to_server(servicer, server):
+ rpc_method_handlers = {
+ 'GetFeature': grpc.unary_unary_rpc_method_handler(
+ servicer.GetFeature,
+ request_deserializer=Point.FromString,
+ response_serializer=Feature.SerializeToString,
+ ),
+ 'ListFeatures': grpc.unary_stream_rpc_method_handler(
+ servicer.ListFeatures,
+ request_deserializer=Rectangle.FromString,
+ response_serializer=Feature.SerializeToString,
+ ),
+ 'RecordRoute': grpc.stream_unary_rpc_method_handler(
+ servicer.RecordRoute,
+ request_deserializer=Point.FromString,
+ response_serializer=RouteSummary.SerializeToString,
+ ),
+ 'RouteChat': grpc.stream_stream_rpc_method_handler(
+ servicer.RouteChat,
+ request_deserializer=RouteNote.FromString,
+ response_serializer=RouteNote.SerializeToString,
+ ),
+ }
+ generic_handler = grpc.method_handlers_generic_handler(
+ 'routeguide.RouteGuide', rpc_method_handlers)
+ server.add_generic_rpc_handlers((generic_handler,))
+
+
class BetaRouteGuideServicer(object):
"""Interface exported by the server.
"""
@@ -320,10 +429,11 @@ class BetaRouteGuideServicer(object):
"""
context.code(beta_interfaces.StatusCode.UNIMPLEMENTED)
+
class BetaRouteGuideStub(object):
"""Interface exported by the server.
"""
- def GetFeature(self, request, timeout):
+ def GetFeature(self, request, timeout, metadata=None, with_call=False, protocol_options=None):
"""A simple RPC.
Obtains the feature at a given position.
@@ -333,7 +443,7 @@ class BetaRouteGuideStub(object):
"""
raise NotImplementedError()
GetFeature.future = None
- def ListFeatures(self, request, timeout):
+ def ListFeatures(self, request, timeout, metadata=None, with_call=False, protocol_options=None):
"""A server-to-client streaming RPC.
Obtains the Features available within the given Rectangle. Results are
@@ -342,7 +452,7 @@ class BetaRouteGuideStub(object):
huge number of features.
"""
raise NotImplementedError()
- def RecordRoute(self, request_iterator, timeout):
+ def RecordRoute(self, request_iterator, timeout, metadata=None, with_call=False, protocol_options=None):
"""A client-to-server streaming RPC.
Accepts a stream of Points on a route being traversed, returning a
@@ -350,7 +460,7 @@ class BetaRouteGuideStub(object):
"""
raise NotImplementedError()
RecordRoute.future = None
- def RouteChat(self, request_iterator, timeout):
+ def RouteChat(self, request_iterator, timeout, metadata=None, with_call=False, protocol_options=None):
"""A Bidirectional streaming RPC.
Accepts a stream of RouteNotes sent while a route is being traversed,
@@ -358,26 +468,19 @@ class BetaRouteGuideStub(object):
"""
raise NotImplementedError()
+
def beta_create_RouteGuide_server(servicer, pool=None, pool_size=None, default_timeout=None, maximum_timeout=None):
- import route_guide_pb2
- import route_guide_pb2
- import route_guide_pb2
- import route_guide_pb2
- import route_guide_pb2
- import route_guide_pb2
- import route_guide_pb2
- import route_guide_pb2
request_deserializers = {
- ('routeguide.RouteGuide', 'GetFeature'): route_guide_pb2.Point.FromString,
- ('routeguide.RouteGuide', 'ListFeatures'): route_guide_pb2.Rectangle.FromString,
- ('routeguide.RouteGuide', 'RecordRoute'): route_guide_pb2.Point.FromString,
- ('routeguide.RouteGuide', 'RouteChat'): route_guide_pb2.RouteNote.FromString,
+ ('routeguide.RouteGuide', 'GetFeature'): Point.FromString,
+ ('routeguide.RouteGuide', 'ListFeatures'): Rectangle.FromString,
+ ('routeguide.RouteGuide', 'RecordRoute'): Point.FromString,
+ ('routeguide.RouteGuide', 'RouteChat'): RouteNote.FromString,
}
response_serializers = {
- ('routeguide.RouteGuide', 'GetFeature'): route_guide_pb2.Feature.SerializeToString,
- ('routeguide.RouteGuide', 'ListFeatures'): route_guide_pb2.Feature.SerializeToString,
- ('routeguide.RouteGuide', 'RecordRoute'): route_guide_pb2.RouteSummary.SerializeToString,
- ('routeguide.RouteGuide', 'RouteChat'): route_guide_pb2.RouteNote.SerializeToString,
+ ('routeguide.RouteGuide', 'GetFeature'): Feature.SerializeToString,
+ ('routeguide.RouteGuide', 'ListFeatures'): Feature.SerializeToString,
+ ('routeguide.RouteGuide', 'RecordRoute'): RouteSummary.SerializeToString,
+ ('routeguide.RouteGuide', 'RouteChat'): RouteNote.SerializeToString,
}
method_implementations = {
('routeguide.RouteGuide', 'GetFeature'): face_utilities.unary_unary_inline(servicer.GetFeature),
@@ -388,26 +491,19 @@ def beta_create_RouteGuide_server(servicer, pool=None, pool_size=None, default_t
server_options = beta_implementations.server_options(request_deserializers=request_deserializers, response_serializers=response_serializers, thread_pool=pool, thread_pool_size=pool_size, default_timeout=default_timeout, maximum_timeout=maximum_timeout)
return beta_implementations.server(method_implementations, options=server_options)
+
def beta_create_RouteGuide_stub(channel, host=None, metadata_transformer=None, pool=None, pool_size=None):
- import route_guide_pb2
- import route_guide_pb2
- import route_guide_pb2
- import route_guide_pb2
- import route_guide_pb2
- import route_guide_pb2
- import route_guide_pb2
- import route_guide_pb2
request_serializers = {
- ('routeguide.RouteGuide', 'GetFeature'): route_guide_pb2.Point.SerializeToString,
- ('routeguide.RouteGuide', 'ListFeatures'): route_guide_pb2.Rectangle.SerializeToString,
- ('routeguide.RouteGuide', 'RecordRoute'): route_guide_pb2.Point.SerializeToString,
- ('routeguide.RouteGuide', 'RouteChat'): route_guide_pb2.RouteNote.SerializeToString,
+ ('routeguide.RouteGuide', 'GetFeature'): Point.SerializeToString,
+ ('routeguide.RouteGuide', 'ListFeatures'): Rectangle.SerializeToString,
+ ('routeguide.RouteGuide', 'RecordRoute'): Point.SerializeToString,
+ ('routeguide.RouteGuide', 'RouteChat'): RouteNote.SerializeToString,
}
response_deserializers = {
- ('routeguide.RouteGuide', 'GetFeature'): route_guide_pb2.Feature.FromString,
- ('routeguide.RouteGuide', 'ListFeatures'): route_guide_pb2.Feature.FromString,
- ('routeguide.RouteGuide', 'RecordRoute'): route_guide_pb2.RouteSummary.FromString,
- ('routeguide.RouteGuide', 'RouteChat'): route_guide_pb2.RouteNote.FromString,
+ ('routeguide.RouteGuide', 'GetFeature'): Feature.FromString,
+ ('routeguide.RouteGuide', 'ListFeatures'): Feature.FromString,
+ ('routeguide.RouteGuide', 'RecordRoute'): RouteSummary.FromString,
+ ('routeguide.RouteGuide', 'RouteChat'): RouteNote.FromString,
}
cardinalities = {
'GetFeature': cardinality.Cardinality.UNARY_UNARY,
diff --git a/examples/python/route_guide/route_guide_resources.py b/examples/python/route_guide/route_guide_resources.py
index 30c7711019..30c7711019 100755..100644
--- a/examples/python/route_guide/route_guide_resources.py
+++ b/examples/python/route_guide/route_guide_resources.py
diff --git a/examples/python/route_guide/route_guide_server.py b/examples/python/route_guide/route_guide_server.py
index 2d8b33ac17..4e780a70a1 100644
--- a/examples/python/route_guide/route_guide_server.py
+++ b/examples/python/route_guide/route_guide_server.py
@@ -29,9 +29,12 @@
"""The Python implementation of the gRPC route guide server."""
+from concurrent import futures
import time
import math
+import grpc
+
import route_guide_pb2
import route_guide_resources
@@ -121,7 +124,9 @@ class RouteGuideServicer(route_guide_pb2.BetaRouteGuideServicer):
def serve():
- server = route_guide_pb2.beta_create_RouteGuide_server(RouteGuideServicer())
+ server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
+ route_guide_pb2.add_RouteGuideServicer_to_server(
+ RouteGuideServicer(), server)
server.add_insecure_port('[::]:50051')
server.start()
try:
diff --git a/examples/python/route_guide/run_codegen.sh b/examples/python/route_guide/run_codegen.py
index a377a1ab40..c7c6008580 100755..100644
--- a/examples/python/route_guide/run_codegen.sh
+++ b/examples/python/route_guide/run_codegen.py
@@ -1,4 +1,3 @@
-#!/bin/bash
# Copyright 2015, Google Inc.
# All rights reserved.
#
@@ -28,5 +27,16 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-# Runs the protoc with gRPC plugin to generate protocol messages and gRPC stubs.
-python -m grpc.tools.protoc -I../../protos --python_out=. --grpc_python_out=. ../../protos/route_guide.proto
+"""Runs protoc with the gRPC plugin to generate messages and gRPC stubs."""
+
+from grpc.tools import protoc
+
+protoc.main(
+ (
+ '',
+ '-I../../protos',
+ '--python_out=.',
+ '--grpc_python_out=.',
+ '../../protos/route_guide.proto',
+ )
+)