Created
August 8, 2017 14:40
-
-
Save gbigwood/d023c21c28e24187efabcb8c4449404c to your computer and use it in GitHub Desktop.
concurrent coroutines demonstrated
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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