Last active
July 19, 2024 02:52
-
-
Save dboyliao/2d68bf2e707c41bc4af6336fccce037f to your computer and use it in GitHub Desktop.
Demo on Impact of Blocking Operation to Async Web Application
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
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"} |
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
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) |
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
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) |
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
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