0

我创建了一个带有循环负载平衡策略的 grpc python 客户端

self.channel = grpc.insecure_channel(self.host,options=[("grpc.lb_policy_name", 
                                                         "round_robin")])

将第一个参数作为主机传递,将第二个参数作为负载平衡策略传递。

self.blocking_stub = dict_pb2_grpc.ExampleServiceStub(self.channel)

也将通道传递给存根。

self.executor = concurrent.futures.ThreadPoolExecutor(
        max_workers=self.thread_pool_size
    )

到目前为止,我很清楚下一步是我想通过executor自定义数量的线程。我怎样才能将 executor 传递给grpc.insecure_channel

我已经经历了(一些事情?),但没有找到与此相关的任何内容。

4

1 回答 1

0

我也在 grpc-io 组上看到了您的帖子:https ://groups.google.com/g/grpc-io/c/uwCRAx1SJFA 。这个问题略有不同。

如何将执行程序传递给 grpc.insecure_channel?

gRPC 客户端不接受 executor 作为输入。gRPC 通道是线程安全的。您可以使用同一通道在多个线程上调用 RPC。

例如,您可以像这样构建代码:

stub = ...
executor = concurrent.futures.ThreadPoolExecutor()

def work():
  response = stub.Greet(...)
  handle_response(response)

for _ in range(100):
  executor.submit(work)
于 2021-12-29T18:45:26.967 回答