【问题标题】:python How to handle grpc stream after stream cancel() is calledpython如何在调用流取消()后处理grpc流
【发布时间】:2019-07-16 06:22:46
【问题描述】:

我正在尝试编写一个 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 代码取消这个流并打印流中的事件吗?

【问题讨论】:

    标签: python grpc grpc-python


    【解决方案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)
    
    

    【讨论】:

    • 感谢您的回复,这对您有帮助
    猜你喜欢
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    • 2021-09-30
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多