Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
#!/usr/bin/env python3
import asyncio
import logging
from pprint import pprint
from reprlib import repr as smart_repr
import requests
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):
loop = asyncio.get_running_loop()
while urls:
url = urls.pop()
# funkce retrieve_user není async, takže ji pustíme přes threadpool executor
user = await loop.run_in_executor(None, retrieve_user, url)
except Exception as e:
logger.error('Failed to download user from %s: %r', url, e)
users[user['id']] = user
def retrieve_user(url):
logger.debug('Downloading %s', url)
response = requests.get(url)
data = response.json()
logger.debug('Downloaded %s -> %s', url, smart_repr(data))
return data['data']
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment