Skip to content

Instantly share code, notes, and snippets.

@gbigwood
Created August 8, 2017 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbigwood/d023c21c28e24187efabcb8c4449404c to your computer and use it in GitHub Desktop.
Save gbigwood/d023c21c28e24187efabcb8c4449404c to your computer and use it in GitHub Desktop.
concurrent coroutines demonstrated
from aiohttp import ClientSession
async def get_file(name, url):
async with ClientSession() as session:
async with session.get(url) as response:
result = await response.read()
print(name, response.status)
async def parallel_on_loop():
await asyncio.gather(
get_file("A", "https://httpbin.org/range/1024?duration=5"),
get_file("B", "http://www.google.com"),
get_file("C", "http://www.bbc.co.uk")
)
loop = asyncio.get_event_loop()
loop.run_until_complete(parallel_on_loop())
loop.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment