Grpc:TypeError:"UnaryStreamCall"对象不可迭代 [英] Grpc: TypeError: 'UnaryStreamCall' object is not iterable

查看:122
本文介绍了Grpc:TypeError:"UnaryStreamCall"对象不可迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python代码:

import asyncio
import logging

import grpc

import helloworld_pb2
import helloworld_pb2_grpc


async def run() -> None:

    async with grpc.aio.insecure_channel('localhost:50051') as channel:
        stub = helloworld_pb2_grpc.GreeterStub(channel)
        it = stub.FindNode(helloworld_pb2.FindNodeRequest())
        for r in it:
            print(r)

if __name__ == '__main__':
    logging.basicConfig()
    asyncio.run(run())

Protobuf:

syntax = "proto3";

option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";

package helloworld;

// The greeting service definition.
service Greeter {

  rpc FindNode(FindNodeRequest) returns (stream FindNodeReply) {}
}



message FindNodeRequest {
    string target=1;
    string from=2;
}

message FindNodeReply {
}

错误:

Traceback (most recent call last):
  File "/Users/me/kad/client.py", line 26, in <module>
    asyncio.run(run())
  File "/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/local/Cellar/python@3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "/Users/me/kad/client.py", line 17, in run
    for r in it:
TypeError: 'UnaryStreamCall' object is not iterable

我该如何解决?

推荐答案

固定.

async for r in stub.FindNode(helloworld_pb2.FindNodeRequest()):
     print(r)

这篇关于Grpc:TypeError:"UnaryStreamCall"对象不可迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆