是否可以在不放入睡眠条件的情况下保持gRPC服务器活着?

时间:2018-06-06 13:26:22

标签: python grpc

请参考以下代码:

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)

***********************************

在上面的代码块中,可以不放入睡眠状态,但服务器仍然存活?

1 个答案:

答案 0 :(得分:3)

目前,gRPC Python服务器没有与C ++服务器Wait() API等效的服务器。您需要保留对Python time.sleep的调用,如我们的example server code