[PYTHON] 5 minute gRPC tutorial

Overview

Here's a summary of what I've researched about gRPC. I also tried running the official tutorial in python.

I will update it as soon as I touch it.

What is gRPC?

gRPC is an open source, sophisticated Remote Procedure Call framework. gRPC makes communication between client / server applications transparent and easy to build.

There is. I'm curious, so take a look at the Official Website.

What makes me happy with gRPC

With gRPC, you can call remote server functions as if they were local, which makes it easier to create distributed services and applications. Since you can write in a language that supports gPRC, you can easily write the gPRC server in Java, client in python, and so on. The complexity of communication between different languages here is handled by gPRC.

About Protocol Buffer

Protocol Buffer is used by default in gPRC (you can also use formats such as json). Protocol Buffer is defined in a .proto file, but its format is very light (3 to 10 times lighter) and faster (20 to 100 times faster) than XML files. If you define the methods used by the service and the structure used for exchanging data in this .proto file, the serialization and deserialization can be easily done through the proto compiler. As a result, accessors (classes) that handle structures transferred in each language are defined, and setters and getters that can set the properties of structures are also created without permission. Therefore, by defining a .proto file, you can easily build services even between languages, and it also absorbs differences such as serialization. Wow! !!

So, let's try the tutorial in Python and C ++.

gRPC tutorial

Python Pip install what you need.

pip install grpcio grpcio-tools

At this time, the latest version may not be included, so be sure to check the version of grpcio properly!

Clone the required repository.

git clone -b v1.28.1 https://github.com/grpc/grpc && cd grpc/examples/python/helloworld

Open two terminals and use one

python greeter_server.py

On the other side

python greeter_client.py

I will try. On the client side

Greeter client received: Hello, you!

If it comes out, it is a success! !!

In addition, let's add another method to the gRPC service.

Here, in addition to the original `SayHello```, add `SayHelloAgain```.

examples/protos/helloworld.proto


// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
  // Sends another greeting
  rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

Update the service.

In ʻexamples / python / helloworld`,

python -m grpc_tools.protoc -I../../protos --python_out=. --grpc_python_out=. ../../protos/helloworld.proto

And Update greeter_server.py and greeter_client.py.

greeter_server.py


class Greeter(helloworld_pb2_grpc.GreeterServicer):

  def SayHello(self, request, context):
    return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)

  def SayHelloAgain(self, request, context):
    return helloworld_pb2.HelloReply(message='Hello again, %s!' % request.name)

greeter_client.py


def run():
  channel = grpc.insecure_channel('localhost:50051')
  stub = helloworld_pb2_grpc.GreeterStub(channel)
  response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
  print("Greeter client received: " + response.message)
  response = stub.SayHelloAgain(helloworld_pb2.HelloRequest(name='you'))
  print("Greeter client received: " + response.message)

If you can do the above,

Open two terminals and use one

python greeter_server.py

On the other side

python greeter_client.py

To execute.

Greeter client received: Hello, you!
Greeter client received: Hello again, you!

It should come out like this!

This time, I investigated gRPC I hope you've run the official tutorial to get a little better understanding of .proto files and more. I will continue to update it as soon as I try various things, or write it in another article! !!

This time around here.

end.

Recommended Posts

5 minute gRPC tutorial
PyODE Tutorial 2
Python tutorial
PyODE Tutorial 1
TensorFlow tutorial tutorial