gRPC Python : Async for alternative?

1,787 views
Skip to first unread message

bdin...@gmail.com

unread,
Nov 29, 2017, 4:29:52 PM11/29/17
to grpc.io

Hi,

Trying to understand the ability for gRPC to use async/await available in python 3.6. Right now I'm modifying the grpc tutorial code to try to understand how async will work.

I'm using the route_guide examples and have modified the code slightly.

Is there a gRPC equivalent for async for ? Right now, this returns the error: "TypeError: 'async for' requires an object with __aiter__ method, got _Rendezvous" , which makes sense. But is there a way around this so that I don't need to block while waiting for responses?

Looked through the github issues, and it seems https://github.com/grpc/grpc/issues/6046 talks about solutions to this, but the solution is to spawn another thread? Can async not be used with gRPC without needing threads?

async def guide_route_chat(stub):
  responses
= stub.RouteChat(generate_messages())
  await asyncio
.sleep(1)
  async
for response in responses:
   
print("Received message %s at %s" % (response.message, response.location))


async
def tasks(num_tasks):
 
for elem in range(num_tasks):
   
print ("task %d" %(elem))
    await asyncio
.sleep(1.0)

def run():
  channel
= grpc.insecure_channel('localhost:50051')
  stub
= route_guide_pb2_grpc.RouteGuideStub(channel)
  asyncio
.ensure_future(guide_route_chat(stub))
  loop
= asyncio.get_event_loop()
  loop
.run_until_complete(tasks(10))

Nathaniel Manista

unread,
Dec 31, 2017, 4:06:20 PM12/31/17
to bdin...@gmail.com, grpc.io
On Wed, Nov 29, 2017 at 1:29 PM, <bdin...@gmail.com> wrote:
Trying to understand the ability for gRPC to use async/await available in python 3.6.

Right now there is no known compatibility.

Right now I'm modifying the grpc tutorial code to try to understand how async will work.

I'm using the route_guide examples and have modified the code slightly.

Is there a gRPC equivalent for async for ?

Not at the moment.

Right now, this returns the error: "TypeError: 'async for' requires an object with __aiter__ method, got _Rendezvous" , which makes sense. But is there a way around this so that I don't need to block while waiting for responses?

The .future methods were intended to cover asynchronous application use cases - so no, you don't have to block while waiting for responses if you don't mind a thread running in the background (one thread per channel no matter how many RPCs are running via the channel).

Looked through the github issues, and it seems https://github.com/grpc/grpc/issues/6046 talks about solutions to this, but the solution is to spawn another thread?

Well, when you call my_stub.MyRpcMethod.future a thread is spawned inside the channel (if needed) so you shouldn't have to spawn any additional thread of your own.

Can async not be used with gRPC without needing threads?

async def guide_route_chat(stub):
  responses
= stub.RouteChat(generate_messages())
  await asyncio
.sleep(1)
  async
for response in responses:
   
print("Received message %s at %s" % (response.message, response.location))


async
def tasks(num_tasks):
 
for elem in range(num_tasks):
   
print ("task %d" %(elem))
    await asyncio
.sleep(1.0)

def run():
  channel
= grpc.insecure_channel('localhost:50051')
  stub
= route_guide_pb2_grpc.RouteGuideStub(channel)
  asyncio
.ensure_future(guide_route_chat(stub))
  loop
= asyncio.get_event_loop()
  loop
.run_until_complete(tasks(10))

As of today asynchronicity in gRPC Python uses threads and requires that threads work the way the Python language standard specifies that they work. Relaxing this restriction is one of the big projects we have planned for 2018.
-N
Reply all
Reply to author
Forward
0 new messages