aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/python/helloworld
diff options
context:
space:
mode:
authorGravatar Nathaniel Manista <nathaniel@google.com>2016-07-15 22:33:50 +0000
committerGravatar Nathaniel Manista <nathaniel@google.com>2016-07-15 22:33:50 +0000
commit46585e23f14b0463c4b7a0d04d72d71228c674ae (patch)
tree761ce6e32c3a8b937b71d0e16d6a67b49d75fa21 /examples/python/helloworld
parent24ae8eaa8d60e7b3b13381179c217796e01b6697 (diff)
Update examples to GA API
Diffstat (limited to 'examples/python/helloworld')
-rw-r--r--examples/python/helloworld/greeter_client.py10
-rw-r--r--examples/python/helloworld/greeter_server.py8
-rw-r--r--examples/python/helloworld/helloworld_pb2.py63
-rw-r--r--[-rwxr-xr-x]examples/python/helloworld/run_codegen.py (renamed from examples/python/helloworld/run_codegen.sh)16
4 files changed, 75 insertions, 22 deletions
diff --git a/examples/python/helloworld/greeter_client.py b/examples/python/helloworld/greeter_client.py
index 40d637fb7b..44d42c102b 100644
--- a/examples/python/helloworld/greeter_client.py
+++ b/examples/python/helloworld/greeter_client.py
@@ -31,17 +31,15 @@
from __future__ import print_function
-from grpc.beta import implementations
+import grpc
import helloworld_pb2
-_TIMEOUT_SECONDS = 10
-
def run():
- channel = implementations.insecure_channel('localhost', 50051)
- stub = helloworld_pb2.beta_create_Greeter_stub(channel)
- response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'), _TIMEOUT_SECONDS)
+ channel = grpc.insecure_channel('localhost:50051')
+ stub = helloworld_pb2.GreeterStub(channel)
+ response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
print("Greeter client received: " + response.message)
diff --git a/examples/python/helloworld/greeter_server.py b/examples/python/helloworld/greeter_server.py
index 2cde5add43..37d8bd49cc 100644
--- a/examples/python/helloworld/greeter_server.py
+++ b/examples/python/helloworld/greeter_server.py
@@ -29,21 +29,25 @@
"""The Python implementation of the GRPC helloworld.Greeter server."""
+from concurrent import futures
import time
+import grpc
+
import helloworld_pb2
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
-class Greeter(helloworld_pb2.BetaGreeterServicer):
+class Greeter(helloworld_pb2.GreeterServicer):
def SayHello(self, request, context):
return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)
def serve():
- server = helloworld_pb2.beta_create_Greeter_server(Greeter())
+ server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
+ helloworld_pb2.add_GreeterServicer_to_server(Greeter(), server)
server.add_insecure_port('[::]:50051')
server.start()
try:
diff --git a/examples/python/helloworld/helloworld_pb2.py b/examples/python/helloworld/helloworld_pb2.py
index 1ee80e4034..3ce33fbf2b 100644
--- a/examples/python/helloworld/helloworld_pb2.py
+++ b/examples/python/helloworld/helloworld_pb2.py
@@ -107,13 +107,55 @@ _sym_db.RegisterMessage(HelloReply)
DESCRIPTOR.has_options = True
DESCRIPTOR._options = _descriptor._ParseOptions(descriptor_pb2.FileOptions(), _b('\n\033io.grpc.examples.helloworldB\017HelloWorldProtoP\001\242\002\003HLW'))
-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 GreeterStub(object):
+ """The greeting service definition.
+ """
+
+ def __init__(self, channel):
+ """Constructor.
+
+ Args:
+ channel: A grpc.Channel.
+ """
+ self.SayHello = channel.unary_unary(
+ '/helloworld.Greeter/SayHello',
+ request_serializer=HelloRequest.SerializeToString,
+ response_deserializer=HelloReply.FromString,
+ )
+
+
+class GreeterServicer(object):
+ """The greeting service definition.
+ """
+
+ def SayHello(self, request, context):
+ """Sends a greeting
+ """
+ context.set_code(grpc.StatusCode.UNIMPLEMENTED)
+ context.set_details('Method not implemented!')
+ raise NotImplementedError('Method not implemented!')
+
+
+def add_GreeterServicer_to_server(servicer, server):
+ rpc_method_handlers = {
+ 'SayHello': grpc.unary_unary_rpc_method_handler(
+ servicer.SayHello,
+ request_deserializer=HelloRequest.FromString,
+ response_serializer=HelloReply.SerializeToString,
+ ),
+ }
+ generic_handler = grpc.method_handlers_generic_handler(
+ 'helloworld.Greeter', rpc_method_handlers)
+ server.add_generic_rpc_handlers((generic_handler,))
+
+
class BetaGreeterServicer(object):
"""The greeting service definition.
"""
@@ -122,23 +164,23 @@ class BetaGreeterServicer(object):
"""
context.code(beta_interfaces.StatusCode.UNIMPLEMENTED)
+
class BetaGreeterStub(object):
"""The greeting service definition.
"""
- def SayHello(self, request, timeout):
+ def SayHello(self, request, timeout, metadata=None, with_call=False, protocol_options=None):
"""Sends a greeting
"""
raise NotImplementedError()
SayHello.future = None
+
def beta_create_Greeter_server(servicer, pool=None, pool_size=None, default_timeout=None, maximum_timeout=None):
- import helloworld_pb2
- import helloworld_pb2
request_deserializers = {
- ('helloworld.Greeter', 'SayHello'): helloworld_pb2.HelloRequest.FromString,
+ ('helloworld.Greeter', 'SayHello'): HelloRequest.FromString,
}
response_serializers = {
- ('helloworld.Greeter', 'SayHello'): helloworld_pb2.HelloReply.SerializeToString,
+ ('helloworld.Greeter', 'SayHello'): HelloReply.SerializeToString,
}
method_implementations = {
('helloworld.Greeter', 'SayHello'): face_utilities.unary_unary_inline(servicer.SayHello),
@@ -146,14 +188,13 @@ def beta_create_Greeter_server(servicer, pool=None, pool_size=None, default_time
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_Greeter_stub(channel, host=None, metadata_transformer=None, pool=None, pool_size=None):
- import helloworld_pb2
- import helloworld_pb2
request_serializers = {
- ('helloworld.Greeter', 'SayHello'): helloworld_pb2.HelloRequest.SerializeToString,
+ ('helloworld.Greeter', 'SayHello'): HelloRequest.SerializeToString,
}
response_deserializers = {
- ('helloworld.Greeter', 'SayHello'): helloworld_pb2.HelloReply.FromString,
+ ('helloworld.Greeter', 'SayHello'): HelloReply.FromString,
}
cardinalities = {
'SayHello': cardinality.Cardinality.UNARY_UNARY,
diff --git a/examples/python/helloworld/run_codegen.sh b/examples/python/helloworld/run_codegen.py
index 34224e5c41..4835ec2b4d 100755..100644
--- a/examples/python/helloworld/run_codegen.sh
+++ b/examples/python/helloworld/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/helloworld.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/helloworld.proto',
+ )
+)