How can I use gRPC with asyncio

左心房为你撑大大i 提交于 2020-07-22 04:37:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!