python를 사용하여 grpc 서비스 제공
1. 설치
pip install grpcio
pip install grpcio-tools
pip install protobuf
pip install flask
2. protobuf 로 IDL 정의
syntax = "proto3";
package demo;
message RequestData {
string data = 1;
}
message ResponseData {
int64 return_code = 1;
string message = 2;
string data = 3;
}
service DemoService {
rpc CreateOne(RequestData) returns (ResponseData) {}
rpc DeleteOne(RequestData) returns (ResponseData) {}
rpc TransferOne(RequestData) returns (ResponseData) {}
rpc GetCreateNotify(RequestData) returns (ResponseData) {}
}
3. grpc compile
python -m grpc_tools.protoc -I/usr/local/include -I. \
--grpc_python_out=. \
--python_out=. \
-I./proto/ demo.proto
4. flask 사용
==============================
from flask import Flask
app = Flask(__name__)
import grpc
import demo_pb2
import demo_pb2_grpc
@app.route('/grpc')
def hello_world_grpc():
with grpc.insecure_channel('127.0.0.1:9090') as channel:
client = demo_pb2_grpc.DemoServiceStub(channel)
response = client.CreateOne(demo_pb2.RequestData(
data="call create one from client",
))
print(response.return_code, response.message, response.data)
return 'Hello, World grpc!'
@app.route('/')
def hello_world():
return 'Hello, World!'
5. grpc gateway
restful api 서비스를 제공하고자 할때 사용
https://github.com/grpc-ecosystem/grpc-gateway
'Language > python' 카테고리의 다른 글
python code test (0) | 2019.09.04 |
---|---|
python 데이터 분석툴 (0) | 2019.06.21 |
python performance and optimization (0) | 2019.03.29 |
timestamp and timezone (0) | 2019.03.29 |
유용한 python 라이브러리 (0) | 2019.03.29 |