我正在关注这个 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
stub 上似乎没有read()
或write()
,一切似乎都是用迭代器实现的。
如何从 this_is_just_read_maybe(stub)
发送消息?
这是正确的方法吗?
我的 Proto 是一个双向流:
service MyService {
rpc MyEventStream (stream StreamingMessage) returns (stream StreamingMessage) {}
}
最佳答案
您尝试做的事情是完全可能的,并且可能涉及编写您自己的请求 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 询问你的对象它想要发送的下一个请求和你的响应(实际上)“等待,坚持,在看到下一个响应结果之前,我不知道我要发送什么请求。”
关于python - 我如何使用 Python gRPC 处理流消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47831895/