Add concurrency into python example in route_guide (#31724)

* Add concurrency into python example in route_guide

- replace await single tasks sequentially with await task_group

* Add comments for asyncio.gather for concurrency

* Correct typo

* Update asyncio_route_guide to meet google style

Co-authored-by: Richard Belleville <rbellevi@google.com>
pull/31955/head
Ho Ngok Chao 2 years ago committed by GitHub
parent 60e4aea792
commit 28e49b0f6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      examples/python/route_guide/asyncio_route_guide_client.py

@ -46,10 +46,16 @@ async def guide_get_one_feature(stub: route_guide_pb2_grpc.RouteGuideStub,
async def guide_get_feature(stub: route_guide_pb2_grpc.RouteGuideStub) -> None:
await guide_get_one_feature(
stub, route_guide_pb2.Point(latitude=409146138, longitude=-746188906))
await guide_get_one_feature(stub,
route_guide_pb2.Point(latitude=0, longitude=0))
# The following two coroutines will be wrapped in a Future object
# and scheduled in the event loop so that they can run concurrently
task_group = asyncio.gather(
guide_get_one_feature(
stub, route_guide_pb2.Point(latitude=409146138,
longitude=-746188906)),
guide_get_one_feature(stub,
route_guide_pb2.Point(latitude=0, longitude=0)))
# Wait until the Future is resolved
await task_group
# Performs a server-streaming call

Loading…
Cancel
Save