【问题标题】:While a gRPC server reads from a stream, would it wait and let the server handle request from other clients?当 gRPC 服务器从流中读取数据时,它会等待并让服务器处理来自其他客户端的请求吗?
【发布时间】:2020-10-26 09:41:51
【问题描述】:

来自官方 gRPC c++ 示例:https://github.com/grpc/grpc/tree/v1.33.1/examples/cpp/route_guide

route_guide_server.cc:

  ...

  Status RouteChat(ServerContext* context,
                   ServerReaderWriter<RouteNote, RouteNote>* stream) override {
    RouteNote note;
    while (stream->Read(&note)) {
      std::unique_lock<std::mutex> lock(mu_);
      for (const RouteNote& n : received_notes_) {
        if (n.location().latitude() == note.location().latitude() &&
            n.location().longitude() == note.location().longitude()) {
          stream->Write(n);
        }
      }
      received_notes_.push_back(note);
    }

    return Status::OK;
  }

  ...

服务器使用 while 循环从流中读取。那么,如果流仍然打开并且服务器正在等待来自客户端的消息,它是否会切换到处理来自其他客户端的请求,因为我的服务器旨在同时与多个客户端通信?

如果不是,有什么更好的方法让服务器等待来自流的消息,同时在等待期间能够与其他客户端交互?

【问题讨论】:

    标签: c++ server grpc


    【解决方案1】:

    在这种情况下,您可以使用异步 gRPC 服务器。 doc

    https://github.com/grpc/grpc/blob/master/test/cpp/qps/server_async.cc 可能是一个示例,以了解如何使用它,但请记住,这是不可理解的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-15
      相关资源
      最近更新 更多