Where can I find examples of using gRPC with asyncio In particular, how to create a client using gRPC and asyncio
Asked
Active
Viewed 1.6k times
5
-
From the [help/on-topic]: "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – ChrisGPT was on strike Dec 22 '18 at 18:15
3 Answers
15
As of version 1.32, gRPC now supports asyncio in its Python API. If you're using an earlier version, you can still use the asyncio API via the experimental API: from grpc.experimental import aio
. An asyncio hello world example has also been added to the gRPC repo. The following code is a copy of the example client:
import logging
import asyncio
from grpc import aio
import helloworld_pb2
import helloworld_pb2_grpc
async def run():
# NOTE(gRPC Python Team): .close() is possible on a channel and should be
# used in circumstances in which the with statement does not fit the needs
# of the code.
async with aio.insecure_channel('localhost:50051') as channel:
stub = helloworld_pb2_grpc.GreeterStub(channel)
response = await stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
print("Greeter client received: " + response.message)
if __name__ == '__main__':
logging.basicConfig()
asyncio.run(run())
See my other answer for how to implement the server.

alan
- 3,246
- 1
- 32
- 36
-
It looks as though this will be moved out of "experimental" in the next release (1.32.0). – Michael Sep 02 '20 at 05:31
-
How can I use it without the "async with"? Because I have a function, when I call this function and pass a parameter, I will send this parameter to rpc. I can not make a connect on every function call. I have to keep a connect and reuse it all the time. – Kingname Sep 16 '21 at 08:14
-
@Kingname `aio.insecure_channel` just returns an `aio.Channel` object. If you look at [the docs](https://grpc.github.io/grpc/python/grpc_asyncio.html#channel-object) for `aio.Channel`, you'll see that the `async with` is not strictly necessary; all that syntax does is close the channel when the `async with` block is done. So you can make a channel with `channel = aio.insecure_channel('localhost:50051')`, you'll just have to close it yourself when you're done with `await channel.close()`. – alan Sep 16 '21 at 16:05
-
Yes, I have noticed that and code by myself. I'd like to know what will happen if I do not close this channel when I kill the process. – Kingname Sep 17 '21 at 07:00
-
@Kingname It looks like `aio.Channel` doesn't handle this scenario, so I assume the connection will die when Python stops the event loop, meaning any in-flight or pending requests will be discarded. If you want to finish those requests you could write a context manager for your program that calls `close` on the channel (with a grace period) and registers your manager's `__exit__` method to be fired on the `SIGINT` signal: https://aalvarez.me/posts/gracefully-exiting-python-context-managers-on-ctrl-c/ – alan Sep 17 '21 at 15:52
2
gRPC Python is currently not compatible with asyncio. See dicussion/feature request at https://github.com/grpc/grpc/issues/6046.

Eric G
- 4,018
- 4
- 20
- 23
1
gRPC support for AsyncIO is now generally available, you can find the documentation here: https://grpc.github.io/grpc/python/grpc_asyncio.html

ButterDog
- 5,115
- 6
- 43
- 61