问题
Where can I find examples of using gRPC with asyncio In particular, how to create a client using gRPC and asyncio
回答1:
gRPC Python is currently not compatible with asyncio. See dicussion/feature request at https://github.com/grpc/grpc/issues/6046.
回答2:
The feature request mentioned by @Eric G has resulted in an asyncio API being added to the gRPC Python API. This API, however, is still experimental, so the only examples currently available are tests buried in the gRPC repo. Since you're wondering how to make an asyncio gRPC client, here's an asyncio adaptation of the hello world example I made:
import logging
import asyncio
from grpc.experimental import aio
import helloworld_pb2
import helloworld_pb2_grpc
async def _make_request():
channel = aio.insecure_channel('localhost:50051')
await channel.channel_ready()
stub = helloworld_pb2_grpc.GreeterStub(channel)
response = await stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
print("Greeter client received: " + response.message)
def main():
asyncio.run(_make_request())
if __name__ == '__main__':
logging.basicConfig()
main()
See my other answer for how to implement the server.
来源:https://stackoverflow.com/questions/53898185/how-can-i-use-grpc-with-asyncio