如何使用Python gRPC处理流式消息

时间:2017-12-15 11:56:22

标签: python stream grpc

我跟随此Route_Guide sample

相关示例会触发并读取消息,而不会回复特定消息。后者是我想要实现的目标。

这是我到目前为止所拥有的:

import grpc
...

channel = grpc.insecure_channel(conn_str)
try:
    grpc.channel_ready_future(channel).result(timeout=5)
except grpc.FutureTimeoutError:
    sys.exit('Error connecting to server')
else:
    stub = MyService_pb2_grpc.MyServiceStub(channel)
    print('Connected to gRPC server.')
    this_is_just_read_maybe(stub)


def this_is_just_read_maybe(stub):
    responses = stub.MyEventStream(stream())
    for response in responses:
        print(f'Received message: {response}')
        if response.something:
            # okay, now what? how do i send a message here?

def stream():
    yield my_start_stream_msg
    # this is fine, i receive this server-side
    # but i can't check for incoming messages here

我似乎没有在存根上有read()write(),所有内容似乎都是用迭代器实现的。

如何从this_is_just_read_maybe(stub)发送消息? 这甚至是正确的方法吗?

My Proto是双向流:

service MyService {
  rpc MyEventStream (stream StreamingMessage) returns (stream StreamingMessage) {}
}

2 个答案:

答案 0 :(得分:4)

您尝试做的事情是完全可能的,并且可能涉及编写您自己的请求iterator对象,可以在它们到达时给出响应,而不是使用简单的生成器作为请求迭代器。也许像是

class MySmarterRequestIterator(object):

    def __init__(self):
        self._lock = threading.Lock()
        self._responses_so_far = []

    def __iter__(self):
        return self

    def _next(self):
        # some logic that depends upon what responses have been seen
        # before returning the next request message
        return <your message value>

    def __next__(self):  # Python 3
        return self._next()

    def next(self):  # Python 2
        return self._next()

    def add_response(self, response):
        with self._lock:
            self._responses.append(response)

然后你使用

my_smarter_request_iterator = MySmarterRequestIterator()
responses = stub.MyEventStream(my_smarter_request_iterator)
for response in responses:
    my_smarter_request_iterator.add_response(response)

。您的_next实现中可能存在锁定和阻塞,以处理gRPC Python的情况,询问您的对象是否要发送下一个请求以及您的响应(实际上)“等待,等等,我不要知道我要发送什么请求,直到我看到下一个响应结果如何“。

答案 1 :(得分:0)

除了编写自定义迭代器外,您还可以使用阻塞队列来实现客户端存根的发送和接收行为:

import queue
...

send_queue = queue.SimpleQueue()  # or Queue if using Python before 3.7
my_event_stream = stub.MyEventStream(iter(send_queue.get, None))

# send
send_queue.push(StreamingMessage())

# receive
response = next(my_event_stream)  # type: StreamingMessage

这利用iter的哨兵形式,它将常规函数转换为迭代器,该迭代器在达到哨兵值(在本例中为None时停止)。