在python

时间:2018-06-28 21:14:41

标签: concurrency grpc gevent

我正在尝试在python中编写高效的grpc客户端实现流式传输。

这就是我的想法

class Client(object):

  def __init__(self):
    #initialize the channel and stub
    self._stub = stub

    # To buffer the requests
    self._buffer = []  

    # gevent pool
    self._pool = pool.Pool(num_senders)

  def send(self, req):
    self._buffer.append(req)

    if len(self._buffer) == self._buffer_flush_threshold:
        buffer_to_flush = self._buffer.copy()
        self._buffer = []

        # Spawn a greenlet to handle this batch of events
        self._pool.spawn(self._sender, buffer_to_flush, self._req_timeout_sec)

  def _sender(self, reqs, req_time_out):
      def req_generator():
          for req in reqs:
              yield req

      try:
          self._stub.Stream(req_generator(), req_time_out)
      except grpc.RpcError as e:
          # Handle exception

  def close():
    self._pool.join()

想法是进行批处理,并使用gevent池通过流式rpc调用发送每个批处理。

给定的实现也不是线程安全的。关于如何改善这一点的任何建议,以便它是线程安全的,并且我可以使用流式API更有效地发送请求?

0 个答案:

没有答案