新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python中Protobuf創(chuàng)建服務端代碼
本文教程操作環(huán)境:windows7系統(tǒng)、python 3.9.1,DELL G3電腦。

1、創(chuàng)建和運行 Greeter 服務可以分為兩個部分:
執(zhí)行服務定義的生成的服務接口:完成實際服務“工作”的功能。
在gRPC服務器上運行,監(jiān)聽來自客戶端的請求并傳送服務響應。
2、在當前目錄,打開文件 greeter_server.py,實現(xiàn)一個新的函數(shù):
from concurrent import futures
import time
import grpc
import hello_pb2
import hello_pb2_grpc
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
class Greeter(hello_pb2_grpc.GreeterServicer):
# 工作函數(shù)
def SayHello(self, request, context):
return hello_pb2.HelloReply(message='Hello, %s!' % request.name)
def serve():
# 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() 不會阻塞,如果運行時你的代碼沒有其它的事情可做,你可能需要循環(huán)等待。
try:
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
server.stop(0)
if __name__ == '__main__':
serve()
以上就是python中protobuf創(chuàng)建服務端代碼的方法,希望能對大家有所幫助。更多Python學習指路:創(chuàng)新互聯(lián)Python教程
當前名稱:創(chuàng)新互聯(lián)Python教程:python中Protobuf創(chuàng)建服務端代碼
網(wǎng)站地址:http://www.5511xx.com/article/coejjci.html


咨詢
建站咨詢
