Skip to content

Instantly share code, notes, and snippets.

@msukmanowsky
Created October 23, 2017 04:07
Show Gist options
  • Save msukmanowsky/8c2c698e2c7fd3d6827e7e83e9fa452b to your computer and use it in GitHub Desktop.
Save msukmanowsky/8c2c698e2c7fd3d6827e7e83e9fa452b to your computer and use it in GitHub Desktop.
import asyncio
import aiohttp
import logging
logging.basicConfig(level=logging.DEBUG)
async def main():
async with aiohttp.ClientSession() as session:
people_urls = await get_people_urls(session)
people = [get_person(session, url) for url in people_urls]
people = await asyncio.gather(*people)
print(people)
async def get_people_urls(session):
# Assume this returns
# {
# "data": {
# "urls": [
# "http://example.com/person/1",
# "http://example.com/person/1"
# ]
# }
# }
async with session.get('http://127.0.0.1:8080/people') as response:
data = await response.json()
return data['data']['urls']
async def get_person(session, url):
async with session.get(url) as response:
data = await response.json()
return data
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment