Skip to content

Instantly share code, notes, and snippets.

Last active Jun 19, 2020
What would you like to do?
#!/usr/bin/env python3
from aiohttp import ClientSession
import asyncio
import logging
from pprint import pprint
from reprlib import repr as smart_repr
logger = logging.getLogger(__name__)
async def main():
urls = '''
users = {}
tasks = [asyncio.create_task(retrieve_users(urls, users)) for i in range(4)]
await asyncio.wait(tasks)
async def retrieve_users(urls, users):
async with ClientSession() as session:
while urls:
url = urls.pop()
await retrieve_user(session, url, users)
except Exception as e:
logger.error('Failed to download user from %s: %r', url, e)
async def retrieve_user(session, url, users):
logger.debug('Downloading %s', url)
async with session.get(url) as response:
data = await response.json()
logger.debug('Downloaded %s -> %s', url, smart_repr(data))
users[data['data']['id']] = data['data']
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment