python在调用stream cancel()之后如何处理grpc流

时间:2019-07-16 06:22:46

标签: python grpc grpc-python

我正在尝试编写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"

client.py

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

for event in stream:
    print(event)

有人可以用Python代码帮助我取消此流并在流中打印事件。

1 个答案:

答案 0 :(得分:0)

问题在于您要在实际开始对其进行迭代之前取消流。尝试在另一个线程上异步取消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)