Skip to content
Snippets Groups Projects
Commit 061d288f authored by Nathaniel Manista's avatar Nathaniel Manista
Browse files

Beta Python documentation correction and update

parent f65d3c11
No related branches found
No related tags found
No related merge requests found
...@@ -91,9 +91,6 @@ Which internally invokes the proto-compiler as: ...@@ -91,9 +91,6 @@ Which internally invokes the proto-compiler as:
$ protoc -I ../../protos --python_out=. --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_python_plugin` ../../protos/helloworld.proto $ protoc -I ../../protos --python_out=. --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_python_plugin` ../../protos/helloworld.proto
``` ```
Optionally, you can just skip the code generation step as the generated python module has already
been generated for you (helloworld_pb2.py).
### The client ### The client
Client-side code can be found in [greeter_client.py](greeter_client.py). Client-side code can be found in [greeter_client.py](greeter_client.py).
......
...@@ -29,15 +29,18 @@ ...@@ -29,15 +29,18 @@
"""The Python implementation of the GRPC helloworld.Greeter client.""" """The Python implementation of the GRPC helloworld.Greeter client."""
from grpc.beta import implementations
import helloworld_pb2 import helloworld_pb2
_TIMEOUT_SECONDS = 10 _TIMEOUT_SECONDS = 10
def run(): def run():
with helloworld_pb2.early_adopter_create_Greeter_stub('localhost', 50051) as stub: channel = implementations.insecure_channel('localhost', 50051)
response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'), _TIMEOUT_SECONDS) stub = helloworld_pb2.beta_create_Greeter_stub(channel)
print "Greeter client received: " + response.message response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'), _TIMEOUT_SECONDS)
print "Greeter client received: " + response.message
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -36,15 +36,15 @@ import helloworld_pb2 ...@@ -36,15 +36,15 @@ import helloworld_pb2
_ONE_DAY_IN_SECONDS = 60 * 60 * 24 _ONE_DAY_IN_SECONDS = 60 * 60 * 24
class Greeter(helloworld_pb2.EarlyAdopterGreeterServicer): class Greeter(helloworld_pb2.BetaGreeterServicer):
def SayHello(self, request, context): def SayHello(self, request, context):
return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name) return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)
def serve(): def serve():
server = helloworld_pb2.early_adopter_create_Greeter_server( server = helloworld_pb2.beta_create_Greeter_server(Greeter())
Greeter(), 50051, None, None) server.add_insecure_port('[::]:50051')
server.start() server.start()
try: try:
while True: while True:
......
...@@ -29,7 +29,7 @@ Then change your current directory to `examples/python/route_guide`: ...@@ -29,7 +29,7 @@ Then change your current directory to `examples/python/route_guide`:
$ cd examples/python/route_guide $ cd examples/python/route_guide
``` ```
You also should have the relevant tools installed to generate the server and client interface code - if you don't already, follow the setup instructions in [the Python quick start guide](../python). You also should have the relevant tools installed to generate the server and client interface code - if you don't already, follow the setup instructions in [the Python quick start guide](../helloworld).
## Defining the service ## Defining the service
...@@ -99,12 +99,11 @@ $ protoc -I ../../protos --python_out=. --grpc_out=. --plugin=protoc-gen-grpc=`w ...@@ -99,12 +99,11 @@ $ protoc -I ../../protos --python_out=. --grpc_out=. --plugin=protoc-gen-grpc=`w
Note that as we've already provided a version of the generated code in the example repository, running this command regenerates the appropriate file rather than creates a new one. The generated code file is called `route_guide_pb2.py` and contains: Note that as we've already provided a version of the generated code in the example repository, running this command regenerates the appropriate file rather than creates a new one. The generated code file is called `route_guide_pb2.py` and contains:
- classes for the messages defined in route_guide.proto - classes for the messages defined in route_guide.proto
- abstract classes for the service defined in route_guide.proto - abstract classes for the service defined in route_guide.proto
- `EarlyAdopterRouteGuideServicer`, which defines the interface for implementations of the RouteGuide service - `BetaRouteGuideServicer`, which defines the interface for implementations of the RouteGuide service
- `EarlyAdopterRouteGuideServer`, which may be started and stopped - `BetaRouteGuideStub`, which can be used by clients to invoke RouteGuide RPCs
- `EarlyAdopterRouteGuideStub`, which can be used by clients to invoke RouteGuide RPCs
- functions for application use - functions for application use
- `early_adopter_create_RouteGuide_server`, which creates a gRPC server given an `EarlyAdopterRouteGuideServicer` object - `beta_create_RouteGuide_server`, which creates a gRPC server given a `BetaRouteGuideServicer` object
- `early_adopter_create_RouteGuide_stub`, which can be used by clients to create a stub object - `beta_create_RouteGuide_stub`, which can be used by clients to create a stub object
<a name="server"></a> <a name="server"></a>
## Creating the server ## Creating the server
...@@ -119,11 +118,11 @@ You can find the example `RouteGuide` server in [route_guide_server.py](route_gu ...@@ -119,11 +118,11 @@ You can find the example `RouteGuide` server in [route_guide_server.py](route_gu
### Implementing RouteGuide ### Implementing RouteGuide
`route_guide_server.py` has a `RouteGuideServicer` class that implements the generated interface `route_guide_pb2.EarlyAdopterRouteGuideServicer`: `route_guide_server.py` has a `RouteGuideServicer` class that implements the generated interface `route_guide_pb2.BetaRouteGuideServicer`:
```python ```python
# RouteGuideServicer provides an implementation of the methods of the RouteGuide service. # RouteGuideServicer provides an implementation of the methods of the RouteGuide service.
class RouteGuideServicer(route_guide_pb2.EarlyAdopterRouteGuideServicer): class RouteGuideServicer(route_guide_pb2.BetaRouteGuideServicer):
``` ```
`RouteGuideServicer` implements all the `RouteGuide` service methods. `RouteGuideServicer` implements all the `RouteGuide` service methods.
...@@ -141,7 +140,7 @@ Let's look at the simplest type first, `GetFeature`, which just gets a `Point` f ...@@ -141,7 +140,7 @@ Let's look at the simplest type first, `GetFeature`, which just gets a `Point` f
return feature return feature
``` ```
The method is passed a `route_guide_pb2.Point` request for the RPC, and an `RpcContext` object that provides RPC-specific information such as timeout limits. It returns a `route_guide_pb2.Feature` response. The method is passed a `route_guide_pb2.Point` request for the RPC, and a `ServicerContext` object that provides RPC-specific information such as timeout limits. It returns a `route_guide_pb2.Feature` response.
#### Response-streaming RPC #### Response-streaming RPC
...@@ -212,8 +211,8 @@ Once you have implemented all the `RouteGuide` methods, the next step is to star ...@@ -212,8 +211,8 @@ Once you have implemented all the `RouteGuide` methods, the next step is to star
```python ```python
def serve(): def serve():
server = route_guide_pb2.early_adopter_create_RouteGuide_server( server = route_guide_pb2.beta_create_RouteGuide_server(RouteGuideServicer())
RouteGuideServicer(), 50051, None, None) server.add_insecure_port('[::]:50051')
server.start() server.start()
``` ```
...@@ -228,17 +227,14 @@ You can see the complete example client code in [route_guide_client.py](route_gu ...@@ -228,17 +227,14 @@ You can see the complete example client code in [route_guide_client.py](route_gu
To call service methods, we first need to create a *stub*. To call service methods, we first need to create a *stub*.
We use the `early_adopter_create_RouteGuide_stub` function of the `route_guide_pb2` module, generated from our .proto. We use the `beta_create_RouteGuide_stub` function of the `route_guide_pb2` module, generated from our .proto.
```python ```python
stub = RouteGuide::Stub.new('localhost', 50051) channel = implementations.insecure_channel('localhost', 50051)
stub = beta_create_RouteGuide_stub(channel)
``` ```
The returned object implements all the methods defined by the `EarlyAdopterRouteGuideStub` interface, and is also a [context manager](https://docs.python.org/2/library/stdtypes.html#typecontextmanager). All RPCs invoked on the stub must be invoked within the stub's context, so it is common for stubs to be created and used with a [with statement](https://docs.python.org/2/reference/compound_stmts.html#the-with-statement): The returned object implements all the methods defined by the `BetaRouteGuideStub` interface.
```python
with route_guide_pb2.early_adopter_create_RouteGuide_stub('localhost', 50051) as stub:
```
### Calling service methods ### Calling service methods
...@@ -255,7 +251,7 @@ feature = stub.GetFeature(point, timeout_in_seconds) ...@@ -255,7 +251,7 @@ feature = stub.GetFeature(point, timeout_in_seconds)
An asynchronous call to `GetFeature` is similar, but like calling a local method asynchronously in a thread pool: An asynchronous call to `GetFeature` is similar, but like calling a local method asynchronously in a thread pool:
```python ```python
feature_future = stub.GetFeature.async(point, timeout_in_seconds) feature_future = stub.GetFeature.future(point, timeout_in_seconds)
feature = feature_future.result() feature = feature_future.result()
``` ```
...@@ -276,7 +272,7 @@ route_summary = stub.RecordRoute(point_sequence, timeout_in_seconds) ...@@ -276,7 +272,7 @@ route_summary = stub.RecordRoute(point_sequence, timeout_in_seconds)
``` ```
```python ```python
route_summary_future = stub.RecordRoute.async(point_sequence, timeout_in_seconds) route_summary_future = stub.RecordRoute.future(point_sequence, timeout_in_seconds)
route_summary = route_summary_future.result() route_summary = route_summary_future.result()
``` ```
......
...@@ -32,6 +32,8 @@ ...@@ -32,6 +32,8 @@
import random import random
import time import time
from grpc.beta import implementations
import route_guide_pb2 import route_guide_pb2
import route_guide_resources import route_guide_resources
...@@ -115,15 +117,16 @@ def guide_route_chat(stub): ...@@ -115,15 +117,16 @@ def guide_route_chat(stub):
def run(): def run():
with route_guide_pb2.early_adopter_create_RouteGuide_stub('localhost', 50051) as stub: channel = implementations.insecure_channel('localhost', 50051)
print "-------------- GetFeature --------------" stub = route_guide_pb2.beta_create_RouteGuide_stub(channel)
guide_get_feature(stub) print "-------------- GetFeature --------------"
print "-------------- ListFeatures --------------" guide_get_feature(stub)
guide_list_features(stub) print "-------------- ListFeatures --------------"
print "-------------- RecordRoute --------------" guide_list_features(stub)
guide_record_route(stub) print "-------------- RecordRoute --------------"
print "-------------- RouteChat --------------" guide_record_route(stub)
guide_route_chat(stub) print "-------------- RouteChat --------------"
guide_route_chat(stub)
if __name__ == '__main__': if __name__ == '__main__':
......
This diff is collapsed.
...@@ -65,7 +65,7 @@ def get_distance(start, end): ...@@ -65,7 +65,7 @@ def get_distance(start, end):
R = 6371000; # metres R = 6371000; # metres
return R * c; return R * c;
class RouteGuideServicer(route_guide_pb2.EarlyAdopterRouteGuideServicer): class RouteGuideServicer(route_guide_pb2.BetaRouteGuideServicer):
"""Provides methods that implement functionality of route guide server.""" """Provides methods that implement functionality of route guide server."""
def __init__(self): def __init__(self):
...@@ -121,8 +121,8 @@ class RouteGuideServicer(route_guide_pb2.EarlyAdopterRouteGuideServicer): ...@@ -121,8 +121,8 @@ class RouteGuideServicer(route_guide_pb2.EarlyAdopterRouteGuideServicer):
def serve(): def serve():
server = route_guide_pb2.early_adopter_create_RouteGuide_server( server = route_guide_pb2.beta_create_RouteGuide_server(RouteGuideServicer())
RouteGuideServicer(), 50051, None, None) server.add_insecure_port('[::]:50051')
server.start() server.start()
try: try:
while True: while True:
......
#!/bin/bash #!/bin/bash
# Runs the protoc with gRPC plugin to generate protocol messages and gRPC stubs. # Runs the protoc with gRPC plugin to generate protocol messages and gRPC stubs.
protoc -I . --python_out=. --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_python_plugin` route_guide.proto protoc -I ../../protos --python_out=. --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_python_plugin` ../../protos/route_guide.proto
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment