Skip to content

Instantly share code, notes, and snippets.

@dboyliao
Last active July 19, 2024 02:52
Show Gist options
  • Save dboyliao/2d68bf2e707c41bc4af6336fccce037f to your computer and use it in GitHub Desktop.
Save dboyliao/2d68bf2e707c41bc4af6336fccce037f to your computer and use it in GitHub Desktop.
Demo on Impact of Blocking Operation to Async Web Application
from asyncio import sleep as asleep
from time import sleep
from fastapi import FastAPI
app = FastAPI()
@app.get("/good")
async def good():
await asleep(2)
return {"message": "Good"}
@app.get("/bad")
async def bad():
sleep(2)
return {"message": "Bad"}
WORKERS?=1
NUM_REQ?=1
run:
uvicorn --workers $(WORKERS) app:app
request-bad:
python request.py http://127.0.0.1:8000/bad -n $(NUM_REQ)
request-good:
python request.py http://127.0.0.1:8000/good -n $(NUM_REQ)
import argparse
from concurrent.futures import ThreadPoolExecutor as Pool
from time import time
import requests
def fetch(url):
start = time()
response = requests.get(url)
duration = time() - start
print(duration)
return response.json()
def main(url, n: int = 4):
with Pool(n) as pool:
_ = pool.map(fetch, [url] * n)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("url", type=str, help="The URL to fetch")
parser.add_argument(
"-n", type=int, default=4, help="The number of requests to make"
)
kwargs = vars(parser.parse_args())
main(**kwargs)
fastapi==0.111.0
requests==2.31.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment