代码之家  ›  专栏  ›  技术社区  ›  Raghav salotra

在不设置睡眠条件的情况下保持grpc服务器的活动是可行的吗?

  •  2
  • Raghav salotra  · 技术社区  · 6 年前

    请参考以下代码:

    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)
    

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

    尝试: 虽然真实: 睡眠时间(86400) 除了键盘中断: 服务器。停止(0)

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

    在上面的代码块中是否可以不设置睡眠条件,而服务器仍将处于活动状态?

    1 回复  |  直到 6 年前
        1
  •  4
  •   Eric G    6 年前

    目前GRPC Python服务器没有等效于C++服务器的 Wait() 应用程序编程接口。您需要保持对python的调用 time.sleep 和我们一样 example server code .