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

1、創(chuàng)建和運(yùn)行 Greeter 服務(wù)可以分為兩個部分:
執(zhí)行服務(wù)定義的生成的服務(wù)接口:完成實(shí)際服務(wù)“工作”的功能。
在gRPC服務(wù)器上運(yùn)行,監(jiān)聽來自客戶端的請求并傳送服務(wù)響應(yīng)。
2、在當(dāng)前目錄,打開文件 greeter_server.py,實(shí)現(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 服務(wù)器
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() 不會阻塞,如果運(yùn)行時你的代碼沒有其它的事情可做,你可能需要循環(huán)等待。
try:
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
server.stop(0)
if __name__ == '__main__':
serve()
以上就是python中protobuf創(chuàng)建服務(wù)端代碼的方法,希望能對大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)Python教程
本文題目:創(chuàng)新互聯(lián)Python教程:python中Protobuf創(chuàng)建服務(wù)端代碼
瀏覽地址:http://fisionsoft.com.cn/article/coejjci.html


咨詢
建站咨詢
