Skip to content

Instantly share code, notes, and snippets.

@MichalMazurek
Last active September 8, 2020 11:03
Show Gist options
  • Save MichalMazurek/4a963675531b424c36e382d95eb22778 to your computer and use it in GitHub Desktop.
Save MichalMazurek/4a963675531b424c36e382d95eb22778 to your computer and use it in GitHub Desktop.
from typing import Dict, List
import aiohttp
import asyncio
import time
JOKE_URL = "http://api.icndb.com/jokes/random"
async def download_joke(session: aiohttp.ClientSession) -> Dict[str, str]:
async with session.get(JOKE_URL) as response:
joke = await response.json()
return joke
async def get_jokes(number_of_jokes: int) -> List[Dict[str, str]]:
async with aiohttp.ClientSession() as session:
return await asyncio.gather(*[download_joke(session) for _ in range(number_of_jokes)])
def main(number_of_jokes: int = 10):
jokes = asyncio.run(get_jokes(number_of_jokes))
for joke in jokes:
print(joke)
start_time = time.perf_counter()
main(10)
delta = time.perf_counter() - start_time
print(f"Downloaded in {delta}s")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment