python - python如何在调用流取消()后处理grpc流
问题描述
我正在尝试编写一个 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代码取消这个流并打印流中的事件吗?
解决方案
问题是您在实际开始迭代之前取消了流。尝试在另一个线程上异步取消 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)
推荐阅读
- openssl - openssl 升级 | 验证证书失败
- angular - 如何清理启用服务器端行模型的 ag-grid 中的所有行?
- python - 如何在 requirements.txt 中只包含包的一部分
- graphql - GraphQL 订阅字段无法访问解析器的上下文
- c++ - 带有宏的 C++ Builder __property 定义
- javascript - 使用 React Hooks 成功认证后跳转到主页
- java - 我在这里使用谓词是不好的做法还是额外的?
- php - 将 WordPress 用户电子邮件而不是帐单电子邮件设置为 WooCommerce 结帐字段的默认值
- python - 使用自定义 S3 对象 lambda 服务的 boto3 禁止请求
- reactjs - 用 react.js 创建钢琴,使用状态不清楚