Last active
January 21, 2021 12:26
-
-
Save emptymalei/3fcdc041a9832eee022914573af1bb94 to your computer and use it in GitHub Desktop.
demonstrate how fastapi and aiohttp can work together to make the API faster
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
# This script is based on the article: | |
# https://dev.to/fuadrafid/fastapi-the-good-the-bad-and-the-ugly-20ob | |
# The task is to gather three subreddit top articles and show them as the return | |
# If we do this in squential code, this will be much slows than the gather method used here. | |
from fastapi import FastAPI | |
import json | |
import asyncio | |
import time | |
import aiohttp | |
from aiohttp import ClientSession | |
app = FastAPI() | |
async def get_json(client: ClientSession, url: str) -> bytes: | |
async with client.get(url) as response: | |
assert response.status == 200 | |
return await response.read() | |
async def get_reddit_top(subreddit: str, client: ClientSession, data: dict): | |
data1 = await get_json(client, 'https://www.reddit.com/r/' + subreddit + '/top.json?sort=top&t=day&limit=5') | |
j = json.loads(data1.decode('utf-8')) | |
subreddit_data = [] | |
for i in j['data']['children']: | |
score = i['data']['score'] | |
title = i['data']['title'] | |
link = i['data']['url'] | |
print(str(score) + ': ' + title + ' (' + link + ')') | |
subreddit_data.append(str(score) + ': ' + title + ' (' + link + ')') | |
data[subreddit] = subreddit_data | |
print('DONE:', subreddit + '\n') | |
@app.get("/") | |
async def get_reddit_data_api() -> dict: | |
start_time: float = time.time() | |
client: ClientSession = aiohttp.ClientSession() | |
data: dict = {} | |
await asyncio.gather( | |
get_reddit_top('python', client, data), | |
get_reddit_top('programming', client, data), | |
get_reddit_top('compsci', client, data), | |
) | |
await client.close() | |
print("Got reddit data in ---" + str(time.time() - start_time) + "seconds ---") | |
return data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment