【问题标题】:How can I use gRPC with asyncio如何将 gRPC 与 asyncio 一起使用
【发布时间】:2018-12-22 18:14:22
【问题描述】:

在哪里可以找到使用 gRPC 和 asyncio 的示例,特别是如何使用 gRPC 和 asyncio 创建客户端

【问题讨论】:

  • 来自help center:“要求我们推荐或查找书籍、工具、软件库、教程或其他场外资源的问题对于 Stack Overflow 来说是无关紧要的,因为它们往往会吸引固执己见的人答案和垃圾邮件。相反,请描述问题以及迄今为止为解决问题所做的工作。"

标签: python python-asyncio


【解决方案1】:

从 1.32 版开始,gRPC 现在在其 Python API 中支持 asyncio。如果您使用的是早期版本,您仍然可以通过实验性 API 使用 asyncio API:from grpc.experimental import aio。 asyncio hello world example 也已添加到 gRPC 存储库中。以下代码是示例客户端的副本:

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())

查看my other answer了解如何实现服务器。

【讨论】:

  • 在下一个版本 (1.32.0) 中这似乎将被移出“实验性”。
  • 没有“async with”如何使用它?因为我有一个函数,所以当我调用这个函数并传递一个参数时,我会将这个参数发送给rpc。我无法对每个函数调用进行连接。我必须一直保持连接并重复使用它。
  • @Kingname aio.insecure_channel 只返回一个 aio.Channel 对象。如果您查看the docs 中的aio.Channel,您会发现async with 并不是绝对必要的;语法所做的只是在async with 块完成时关闭通道。所以你可以用channel = aio.insecure_channel('localhost:50051')创建一个频道,当你用完await channel.close()后你只需要自己关闭它。
  • 是的,我注意到了这一点并自己编写了代码。我想知道如果我在终止进程时不关闭此频道会发生什么。
  • @Kingname 看起来aio.Channel 无法处理这种情况,所以我假设当 Python 停止事件循环时连接将终止,这意味着任何正在进行的或未决的请求都将被丢弃。如果你想完成这些请求,你可以为你的程序编写一个上下文管理器,在通道上调用close(带有宽限期)并注册你的管理器的__exit__方法,以便在SIGINT信号上触发:@987654325 @
【解决方案2】:

gRPC Python 目前与 asyncio 不兼容。请参阅https://github.com/grpc/grpc/issues/6046 的讨论/功能请求。

【讨论】:

    【解决方案3】:

    gRPC 对 AsyncIO 的支持现已全面推出,您可以在此处找到文档:https://grpc.github.io/grpc/python/grpc_asyncio.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-25
      • 2018-06-06
      • 2020-07-12
      • 2021-08-07
      • 2022-08-07
      • 2016-03-14
      • 2015-06-22
      • 1970-01-01
      相关资源
      最近更新 更多