网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
int32 feature_count = 2;
int32 distance = 3;
int32 elapsed_time = 4;
}
根据协议文件生成route\_guide\_pb2.py、route\_guide\_pb2\_grpc.py两个必要的模块文件,然后就可以根据他们来创建客户端了。
## 二、客户端实现
##### 1、应答流式RPC
应答流式RPC返回的内容为流式,一次请求,n次返回。我们可以用for循环来接收返回的内容:
def guide_list_features(stub):
rectangle = route_guide_pb2.Rectangle(
lo=route_guide_pb2.Point(latitude=400000000, longitude=-750000000),
hi=route_guide_pb2.Point(latitude=420000000, longitude=-730000000))
print(“Looking for features between 40, -75 and 42, -73”)
features = stub.ListFeatures(rectangle)
for feature in features:
print(“Feature called %s at %s” % (feature.name, feature.location))
##### 2、请求流式RPC
请求流式RPC请求的内容为流式,n次请求,一次返回。我们可以用迭代器来发送若干份请求数据:
def generate_route(feature_list):
for _ in range(0, 10):
random_feature = feature_list[random.randint(0, len(feature_list) - 1)]
print(“Visiting point %s” % random_feature.location)
yield random_feature.location
def guide_record_route(stub):
feature_list = route_guide_resources.read_route_guide_database()
route_iterator = generate_route(feature_list)
route_summary = stub.RecordRoute(route_iterator)
print("Finished trip with %s points " % route_summary.point_count)
print("Passed %s features " % route_summary.feature_count)
print("Travelled %s meters " % route_summary.distance)
print("It took %s seconds " % route_summary.elapsed_time)
其中route\_iterator为一个迭代器。
##### 3、双向流式RPC
双向流式RPC请求的内容为流式,返回内容也为流式,n次请求,n次返回。我们可以用迭代器来发送若干份请求数据,通过for循环来接收返回结果:
def generate_messages():
messages = [
make_route_note(“First message”, 0, 0),
make_route_note(“Second message”, 0, 1),
make_route_note(“Third message”, 1, 0),
make_route_note(“Fourth message”, 0, 0),
make_route_note(“Fifth message”, 1, 0),
]
for msg in messages:
print(“Sending %s at %s” % (msg.message, msg.location))
yield msg
def guide_route_chat(stub):
responses = stub.RouteChat(generate_messages())
for response in responses:
print(“Received message %s at %s” %
(response.message, response.location))
## 三、实际应用
在录音笔项目中,需要对转写后的文本进行分段语义整理,由于文本内容可能较多,服务端需要采用流式的方式进行接收,并通过流式的方式将结果返给客户端,于是这里采用了双向流式RPC形式的接口。
接口协议如下(仅为演示需要,只展示部分内容):
syntax = “proto3”;
package sogou.parrot.inner.semantic.v1;
import “google/protobuf/duration.proto”;
import “record.proto”;
option go_package = “git.speech.sogou/semantic/v1;semantic”;
service discourse_understand{
rpc UnderstandFullText(stream UnderstandFullTextRequest) returns(stream UnderstandFullTextResponse);
}
message UnderstandFullTextRequest{
repeated SubSentence sub_sentences = 1;
repeated sogou.parrot.record.v1.NonSpeechSoundInfo sound_infos = 2;
repeated sogou.parrot.record.v1.AIMark ai_marks = 3;
}
message UnderstandFullTextResponse{
UnderstandFullTextResult result = 2;
}
实现客户端的关键代码如下:
def gen_iterator(request):
for r in [request]:
yield r
def get_understand_full_textresponse(stub, ai_marks, sound_infos, sub_sentences):
request = UnderstandFullTextRequest()
request.sub_sentences.extend(sub_sentences)
request.sound_infos.extend(sound_infos)
request.ai_marks.extend(ai_marks)
request_iter = gen_iterator(request)
try:
resps = stub.UnderstandFullText(request_iter)
for resp in resps:
resp_str = json.dumps(json.loads(MessageToJson(resp)),indent=4, ensure_ascii=False)
print(resp_str)
except Exception as e:
print (e)
def run():
ai_marks, sound_infos, sub_sentences = extract_data()
with grpc.insecure_channel(sys.argv[2]) as channel:
stub = discourse_understandStub(channel)
print(“-------------- UnderstandFullText --------------”)
get_understand_full_textresponse(stub, ai_marks, sound_infos, sub_sentences)
if name == ‘main’:
run()
运行客户端,可以成功返回结果:
![](https://img-blog.csdnimg.cn/c783fadb400d4a198974d89f0bbd294e.png)
进一步,如果需要对接口进行并发下的稳定性测试,依然可以将客户端编译成可执行程序或利用shell脚本,再结合jmeter等自动化测试工具进行测试,以编译可执行程序的方法为例:
首先利用pyinstaller工具将脚本编译为可执行程序,接下来用jmeter编写自动化测试脚本,在线程组下添加OS Process Sampler,传入所需参数(下面的三个参数值为:文本,地址,句子起始编号):
![图片](https://img-blog.csdnimg.cn/img_convert/f125884a0ba75fb2cfc7d51a635d2cdb.jpeg)
运行脚本,即可自动化进行测试并得到结果,从中可以得到性能、稳定性相关指标:
![图片](https://img-blog.csdnimg.cn/img_convert/b84b2afc964a9b5c82f0d1db2b5e247c.jpeg)
![img](https://img-blog.csdnimg.cn/img_convert/b7d2831265802aa15a1f406bee4858be.png)
![img](https://img-blog.csdnimg.cn/img_convert/1768593db2e9a5ff52c18b28a12720cd.png)
**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**
**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
升。**
**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**
**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**