0

我正在尝试编写一个 python 客户端来终止一个 gRPC 流

原型:

rpc Start (StartParameters) returns (stream Progress) {}

在客户端中,当我准备好终止流时,我尝试调用 stream.cancel(),然后当我打印捕获的流的事件时,它不会打印事件。我看到了例外

<_Rendezvous of RPC that terminated with:
    status = StatusCode.CANCELLED
    details = "Locally cancelled by application!"
    debug_error_string = "None"

客户端.py

stream = self.stub.Start(params)
time.sleep(120)
stream.cancel()

for event in stream:
    print(event)

有人可以帮我用python代码取消这个流并打印流中的事件吗?

4

1 回答 1

1

问题是您在实际开始迭代之前取消了流。尝试在另一个线程上异步取消 RPC。

stream = self.stub.Start(params)

def _cancel_after_a_while():
    time.sleep(120)
    stream.cancel()

cancellation_thread = threading.Thread(target=_cancel_after_a_while)
cancellation_thread.start()

for event in stream:
    print(event)

于 2019-07-23T22:55:21.220 回答