grpc流式传输心得

【grpc流式传输心得】https://www.jianshu.com/p/43fdfeb105ff(很好的一篇简书)
https://www.grpc.io/docs/tutorials/basic/python/(官方文档)
实习之后的第一篇博客,导师让我研究grpc流式传输,真的是折腾了n天,慢的像只蜗牛。。。
grpc流式传输心得
文章图片

实现流式传输的关键代码是这一段,在服务器上可以一秒内把110MB的文本传输到另一个文本。

size=2190304 # 把文本转换为request def request_from_db(text): tmp="" for line in text: tmp+=line if len(tmp)>=size: request = helloworld_pb2.HelloRequest(text=tmp) tmp="" yield request request = helloworld_pb2.HelloRequest(text=tmp) yield request

# hello_db.pydef read_hello_db(): with open('/root/model/hello_db.txt','r') as f: for line in f: yield line

helloworld.proto
// [python quickstart](https://grpc.io/docs/quickstart/python.html#run-a-grpc-application) // python -m grpc_tools.protoc --python_out=. --grpc_python_out=. -I. helloworld.proto // python helloworld_grpc_server.py // python helloworld_grpc_client.py // helloworld.proto syntax = "proto3"; service Greeter { rpc HelloChat(stream HelloRequest) returns (stream HelloReply){} }message HelloRequest { string text = 1; }message HelloReply{ string text =1; }

helloworld_grpc_server.py
# -*-coding:utf-8-*- from concurrent import futures import time import grpc import helloworld_pb2 import helloworld_pb2_grpc import hello_db# 实现 proto 文件中定义的 GreeterServicer class Greeter(helloworld_pb2_grpc.GreeterServicer): def __init__(self): self.db = hello_db.read_hello_db() # 实现 proto 文件中定义的 rpc 调用 # 传入request 返回response def HelloChat(self, request_iterator, context): for request in request_iterator: response = helloworld_pb2.HelloReply(text = request.text) yield responsedef serve(): # 启动 rpc 服务 server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server) server.add_insecure_port('[::]:50051') server.start() try: while True: time.sleep(60*60*24) # one day in seconds except KeyboardInterrupt: server.stop(0)if __name__ == '__main__': serve()

helloworld_grpc_client.py
# -*-coding:utf-8-*- import grpc import helloworld_pb2 import helloworld_pb2_grpc import hello_db import time size=2190304 # 把文本转换为request def request_from_db(text): tmp="" for line in text: tmp+=line if len(tmp)>=size: request = helloworld_pb2.HelloRequest(text=tmp) tmp="" yield request request = helloworld_pb2.HelloRequest(text=tmp) yield requestdef run(): # 连接 rpc 服务器 start_time1 = time.time() channel = grpc.insecure_channel('localhost:50051') # 调用 rpc 服务 stub = helloworld_pb2_grpc.GreeterStub(channel) req = request_from_db(hello_db.read_hello_db()) start_time2 = time.time() response = stub.HelloChat(req) elapsed_time2 = int(time.time() - start_time2) print(elapsed_time2) with open('/root/model/write_db.txt', 'w') as f: for item in response: f.write(item.text) elapsed_time1 = int(time.time() - start_time1) print(elapsed_time1)if __name__ == '__main__': run()


2019.7.9开始痛并快乐着的实习生涯,有收获,也有迷茫。加油把!

    推荐阅读