cd examples/python/helloworld
python -m grpc_tools.protoc -I../../protos --python_out=. --pyi_out=. --grpc_python_out=. ../../protos/helloworld.proto
以下のファイルが再作成されている。
1
2
3
4
5
ls -l
-rw-r--r--@ 1 xx xx 182391 18:12 helloworld_pb2.py
-rw-r--r--@ 1 xx xx 57891 18:12 helloworld_pb2.pyi
-rw-r--r--@ 1 xx xx 701891 18:12 helloworld_pb2_grpc.py
fromconcurrentimportfuturesimportloggingimportgrpcimporthelloworld_pb2importhelloworld_pb2_grpcclassGreeter(helloworld_pb2_grpc.GreeterServicer):defSayHello(self,request,context):returnhelloworld_pb2.HelloReply(message="Hello, %s!"%request.name)# 以下の関数を追加defSayHelloAgain(self,request,context):returnhelloworld_pb2.HelloReply(message="Hello Again, %s!"%request.name)defserve():port="50051"server=grpc.server(futures.ThreadPoolExecutor(max_workers=10))helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(),server)server.add_insecure_port("[::]:"+port)server.start()print("Server started, listening on "+port)server.wait_for_termination()if__name__=="__main__":logging.basicConfig()serve()
greeter_client.py を更新する。
1
2
3
4
5
6
7
8
9
10
11
defrun():# NOTE(gRPC Python Team): .close() is possible on a channel and should be# used in circumstances in which the with statement does not fit the needs# of the code.print("Will try to greet world ...")withgrpc.insecure_channel("localhost:50051")aschannel: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)