如何检查gRPC服务器是否在Python中运行?

时间:2018-08-21 15:30:02

标签: grpc grpc-python

据我了解,建议在服务器中运行以下代码以启动gRPC服务器:

def serve():
    # create a gRPC server
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    greet_service = GreetServiceServicer()
    greet_pb2_grpc.add_GreetServiceServicer_to_server(greet_service, server)

    print('Starting server. Listening on port 50051.')
    server.add_insecure_port('[::]:50051')
    server.start()
    # since server.start() will not block,
    # a sleep-loop is added to keep alive
    try:
        while True:
            time.sleep(86400)
    except KeyboardInterrupt:
        server.stop(0)

问题:

  1. 此代码可以并行运行两次。第二台服务器应该不能绑定到50051端口,但是在第二次启动时没有给出错误消息?
  2. 睡眠循环可能无法捕获任何服务器级别的异常或关闭,如何合并这些异常?

0 个答案:

没有答案