请参考以下代码:
import grpc
from concurrent import futures
import time
import calculator_pb2
import calculator_pb2_grpc
import calculator
class CalculatorServicer(calculator_pb2_grpc.CalculatorServicer):
def SquareRoot(selfself, request, context):
response = calculator_pb2.Number()
response.value = calculator.square_root((request.value))
return response
# creating grpc server
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
calculator_pb2_grpc.add_CalculatorServicer_to_server(CalculatorServicer(), server)
print('Starting server. Listening on port 50051.')
server.add_insecure_port('[::]:50051')
server.start()
# below line is what keeping server alive
try:
while True:
time.sleep(86400)
except KeyboardInterrupt:
server.stop(0)
尝试: 而真: time.sleep(86400) 除KeyboardInterrupt外: server.stop(0)
在上面的代码块中,可以不放入睡眠状态,但服务器仍然存活?
答案 0 :(得分:3)
目前,gRPC Python服务器没有与C ++服务器Wait()
API等效的服务器。您需要保留对Python time.sleep
的调用,如我们的example server code。