Reputation: 7343
Python code:
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 {
}
Error:
Traceback (most recent call last):
File "/Users/me/kad/client.py", line 26, in <module>
asyncio.run(run())
File "/usr/local/Cellar/[email protected]/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/[email protected]/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
How do I fix this?
Upvotes: 2
Views: 2943
Reputation: 7343
Fixed.
async for r in stub.FindNode(helloworld_pb2.FindNodeRequest()):
print(r)
Upvotes: 5