I tried gRPC with Python

Install the following on your Mac

$ brew install protobuf
$ pip install grpcio-tools

Prepare a proto file

hoge.proto


syntax = "proto3";

package gateway;

message ReplyRequest {
    string message = 1;
}

message ReplyResponse {
    string message = 1;
}

service AVRGateway {
    rpc Reply (ReplyRequest) returns (ReplyResponse) {}
}

Write protoc settings as a file to generate

codegen.py


from grpc.tools import protoc


protoc.main(
    (
        '',
        '-I.',
        '--python_out=.',
        '--grpc_python_out=.',
        './hoge.proto',
    )
)

Execute the codegen.py created above.

$ python ./codegen.py

Create a py file for the server

grpc_server.py


from concurrent import futures
import time

import grpc

from hoge_pb2 import ReplyResponse
from hoge_pb2_grpc import AVRGatewayServicer
from hoge_pb2_grpc import add_AVRGatewayServicer_to_server

_ONE_DAY_IN_SECONDS = 60 * 60 * 24


class RouteGuideServicer(AVRGatewayServicer):
    def Reply(self, request, context):
        print('reply!')
        return ReplyResponse(message='Hoge')


def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    add_AVRGatewayServicer_to_server(
            RouteGuideServicer(), server)
    server.add_insecure_port('[::]:50051')
    server.start()
    try:
        while True:
            time.sleep(_ONE_DAY_IN_SECONDS)
    except KeyboardInterrupt:
        server.stop(0)


if __name__ == '__main__':
    serve()

Create a py file on the client side

stub.py


from __future__ import print_function
import grpc
import hoge_pb2
import hoge_pb2_grpc


def run():
    channel = grpc.insecure_channel('localhost:50051')
    stub = hoge_pb2_grpc.AVRGatewayStub(channel)
    response = stub.Reply(hoge_pb2.ReplyRequest(message='hoge'))
    print("Greeter client received: " + response.message)


if __name__ == '__main__':
    run()

Let's run client after running server

https://blog.fenrir-inc.com/jp/2016/10/grpc-go.html

Recommended Posts

I tried gRPC with Python
I tried fp-growth with python
I tried scraping with Python
I tried scraping with python
I tried web scraping with python.
I tried running prolog with python 3.8.2.
I tried SMTP communication with Python
I tried scraping Yahoo News with Python
I tried Python> autopep8
I tried sending an email with python.
I tried non-photorealistic rendering with Python + opencv
GRPC starting with Python
I tried a functional language with Python
I tried recursion with Python ② (Fibonacci sequence)
I tried Python> decorator
#I tried something like Vlookup with Python # 2
I tried "smoothing" the image with Python + OpenCV
I tried "differentiating" the image with Python + OpenCV
I tried L-Chika with Raspberry Pi 4 (Python edition)
I tried using mecab with python2.7, ruby2.3, php7
I tried function synthesis and curry with python
I tried to output LLVM IR with Python
I tried "binarizing" the image with Python + OpenCV
I tried running faiss with python, Go, Rust
I tried to automate sushi making with python
I tried playing mahjong with Python (single mahjong edition)
I tried running Deep Floor Plan with Python 3.6.10.
I tried sending an email with SendGrid + Python
I tried Learning-to-Rank with Elasticsearch!
I made blackjack with python!
I tried clustering with PyCaret
I tried Python C extension
I made blackjack with Python.
I made wordcloud with Python.
I tried to implement Minesweeper on terminal with python
I tried to get started with blender python script_Part 01
I tried to touch the CSV file with Python
[OpenCV / Python] I tried image analysis of cells with OpenCV
I tried to solve the soma cube with python
I tried to get started with blender python script_Part 02
I tried to implement an artificial perceptron with python
I tried to automatically generate a password with Python3
Mayungo's Python Learning Episode 1: I tried printing with print
I tried to solve the problem with Python Vol.1
I tried to analyze J League data with Python
I tried "morphology conversion" of images with Python + OpenCV
I tried hitting the API with echonest's python client
I tried to solve AOJ's number theory with Python
I tried to touch Python (installation)
I tried trimming efficiently with OpenCV
I can't install python3 with pyenv-vertualenv
I tried summarizing sentences with summpy
I tried moving food with SinGAN
I made a fortune with Python.
I tried implementing DeepPose with PyTorch
I tried using Thonny (Python / IDE)
I tried Grumpy (Go running Python).
I liked the tweet with python. ..
I tried face detection with MTCNN
I played with PyQt5 and Python3
I want to debug with Python