I'm probably not understanding the asynchronous concept correctly in FastAPI.
I'm accessing the root endpoint of the following app from two clients at the same time. I'd expect FastAPI to print Started
twice in a row at the start of the execution:
from fastapi import FastAPI
import asyncio
app = FastAPI()
@app.get("/")
async def read_root():
print('Started')
await asyncio.sleep(5)
print('Finished')
return {"Hello": "World"}
Instead I get the following, which looks very much non asynchronous:
Started
Finished
INFO: ('127.0.0.1', 49655) - "GET / HTTP/1.1" 200
Started
Finished
INFO: ('127.0.0.1', 49655) - "GET / HTTP/1.1" 200
What am I missing?
Unlike Flask, FastAPI is implemented on ASGI and allows you to create both asynchronous and synchronous applications natively.
Amazing single-threaded performance with FastAPI — optimize your code for a HUGE performance boost! Python has many web frameworks the most popular being Django and Flask.
FastAPI (Async) - Python FastAPI in asynchronous mode clocks in at ~228 requests per second.
Not only is FastAPI intuitive and straight-forward to use in projects, but the FastAPI code also has 100% test coverage hence it's production-ready robust code.
How do you ensure there are multiple simultaneous requests?
Your code is fine - try using following command for testing:
for n in {1..5}; do curl http://localhost:8000/ & ; done
Your browser might be caching subsequent requests to the same URL.
Well from what the demos in this github issue show, it's probably not due to FastAPI but the client that runs the requests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With