use*_*245 3 python python-asyncio
在哪里可以找到将 gRPC 与 asyncio 结合使用的示例,特别是如何使用 gRPC 和 asyncio 创建客户端
ala*_*lan 10
从 1.32 版开始,gRPC 现在在其Python API 中支持 asyncio 。如果您使用的是早期版本,您仍然可以通过实验性 API 使用 asyncio API:from grpc.experimental import aio
。gRPC 存储库中还添加了一个 asyncio hello world 示例。以下代码是示例客户端的副本:
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())
Run Code Online (Sandbox Code Playgroud)
有关如何实现服务器的信息,请参阅我的其他答案。