pythonProtobuf创建服务端代码
python中Protobuf创建服务端代码
本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。
1、创建和运行Greeter服务可以分为两个部分:
执行服务定义的生成的服务接口:完成实际服务“工作”的功能。
在gRPC服务器上运行,监听来自客户端的请求并传送服务响应。
2、在当前目录,打开文件greeter_server.py,实现一个新的函数:
fromconcurrentimportfutures
importtime
importgrpc
importhello_pb2
importhello_pb2_grpc
_ONE_DAY_IN_SECONDS=60*60*24
classGreeter(hello_pb2_grpc.GreeterServicer):
#工作函数
defSayHello(self,request,context):
returnhello_pb2.HelloReply(message='Hello,%s!'%request.name)
defserve():
#gRPC服务器
server=grpc.server(futures.ThreadPoolExecutor(max_workers=10))
hello_pb2_grpc.add_GreeterServicer_to_server(Greeter(),server)
server.add_insecure_port('[::]:50051')
server.start()#start()不会阻塞,如果运行时你的代码没有其它的事情可做,你可能需要循环等待。
try:
whileTrue:
time.sleep(_ONE_DAY_IN_SECONDS)
exceptKeyboardInterrupt:
server.stop(0)
if__name__=='__main__':
serve()
以上就是python中Protobuf创建服务端代码的方法,希望能对大家有所帮助。更多Python学习教程请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
python函数标注是什么
python函数标注是什么在参数的使用方法中,还有一种解包的情况是需要我们掌握的。比如,将列表或者字典的值转换为函数的参数,就需要用到参数解...详情>>
2023-11-14 14:13:35
python正负索引的使用
python正负索引的使用本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、说明使用正索引(从左至右)、负索引(从右至左)访问元素,时...详情>>
2023-11-14 13:45:04
python垃圾回收的机制过程
python垃圾回收的机制过程本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、步骤(1)收集所有容器对象(list,dict,tuple,customClas...详情>>
2023-11-14 13:05:15
pythontry语句的执行原理
pythontry语句的执行原理1、原理分析(1)执行try子句(try和except关键字之间的多行语句)。(2)如果没有异常发生,则跳过except子句并完成try语句...详情>>
2023-11-14 09:37:35热门推荐
技术干货






