简体   繁体   English

grpc 客户端 python:如何创建 grpc 客户端连接池以获得更好的吞吐量?

[英]grpc client python: How to create grpc client connection pool for better throughput?

Our usecase is to make a large number of requests.我们的用例是发出大量请求。 Each request return 1 MB of data.每个请求返回 1 MB 的数据。 Right now, on client side, we create a single GRPC channel and the run the following function in a loop现在,在客户端,我们创建一个单独的 GRPC 通道并在循环中运行以下 function

content_grpc_channel = grpc.insecure_channel(content_netloc)
test_stub = test_pb2_grpc.ContentServiceInternalStub(
    content_grpc_channel)

def get_feature_data_future(feature_id, span_context=()):
  req_feature = test_pb2.GetFeatureRequest()
  req_feature.feature_id = feature_id
  resp_feature_future = test_stub.GetFeature.future(
      req_feature, metadata=span_context)
  return resp_feature_future

My question is in python how I can create grpc client connection pool for better throughput?我的问题是在 python 如何创建 grpc 客户端连接池以获得更好的吞吐量?

In golang I see this https://godoc.org/google.golang.org/api/option#WithGRPCConnectionPool but I have a hard time to find the doc in python.在 golang 中,我看到了这个https://godoc.org/google.golang.org/api/option#WithGRPCConnectionPool但我很难在 python 中找到文档。

Is there such a utility in python to create grpc connection pool? python中是否有这样的实用程序来创建grpc连接池? Or should I create multiple grpc channels and manage those myself?或者我应该创建多个 grpc 频道并自己管理这些频道? I assume each channel will have different tcp connection, correct?我假设每个通道都有不同的 tcp 连接,对吗?

gRPC uses HTTP/2 and can multiplex many requests on one connection and gRPC client connections should be re-used for the lifetime of the client app. gRPC 使用 HTTP/2 并且可以在一个连接上多路复用多个请求,并且 gRPC 客户端连接应该在客户端应用程序的生命周期内重复使用。

The Golang link you mentioned, says that WithGRPCConnectionPool would be used to balance the requests.您提到的 Golang 链接说 WithGRPCConnectionPool 将用于平衡请求。 You might search for load balancing if it is what you need but remember that load balancing only makes sense if you have multiple gRPC server instances.如果需要,您可能会搜索负载平衡,但请记住,负载平衡仅在您有多个 gRPC 服务器实例时才有意义。

If you are searching for a connection pool inspired by what is done when working with databases, I would say you don't need to worry about it as the opening connection overhead doesn't exist when working with gRPC如果您正在寻找一个受数据库操作启发的连接池,我想说您不必担心它,因为使用 gRPC 时不存在打开连接开销

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM