首页 文章

来自Python服务器的gRPC Unary Stream - > C#Client:“Stream removed”

提问于
浏览
0

我有一个从Python服务器(1.8.4)到C#客户端(1.8.3)的一元流服务 .

当我发信号通知Python关闭(TERM15)时,调用下面代码清单中的 shutdown 方法,其目的是正常终止RPC,并关闭服务器 .

这在我在localhost上运行服务器时有效,会引发具有预期状态的RpcException: Status(StatusCode=Cancelled, Detail="Completed")

但是,当在Kubernetes(GKE)中运行服务器并终止我在客户端中收到的 Grpc.Core.RpcException: Status(StatusCode=Unknown, Detail="Stream removed") . python服务器位于haproxy入口控制器和google-cloud-endpoint代理之后,但据我所知,这些组件都不会影响连接 .

任何人都可以想到可能导致客户端RpcException中的不同状态的原因是什么?

class StreamsService(StreamsServicer):
    def MyHandler(self, request, context):
        while not stop_event.isSet():
            try:
                yield update_q.get(True, 0.1)
            except queue.Empty:
                continue

        context.set_code(grpc.StatusCode.CANCELLED)
        context.set_details("Completed")

def shutdown(subscriber_service: StreamsService,
             executor: futures.ThreadPoolExecutor,
             server: grpc.Server, exit_code):    

    logger.info("Stopping stream handlers")
    for stop_event in subscriber_service.stop_events:
        stop_event.set()

    logger.info("Stopping executor")
    executor.shutdown()

    logger.info("Stopping server")
    ev: threading.Event = server.stop(grace=10)  # allows RPCs to terminate gracefully

    logger.info("Waiting for server to stop gracefully")
    ev.wait()

    logger.info("Stopping process with exit code {}".format(exit_code))
    sys.exit(exit_code)

(ps . 交叉发布自https://groups.google.com/forum/#!topic/grpc-io/Ge6hcUUhXzo

1 回答

相关问题