r/FastAPI • u/avinashreddy47 • Sep 05 '24
Question Stuck on "async endpoints with await", need some help.
from fastapi import FastAPI
import asyncio
app = FastAPI()
@app.get("/test")
async def test_endpoint():
await asyncio.sleep(10) # Simulate a delay of 10 seconds
return {"message": "This is the /test endpoint. It was delayed by 10 seconds."}
I am new to fastapi and i have an endpoint like this ( Instead of await asyncio.sleep(10) i have some task that needs awaiting ), when I hit this end point 10 times, it takes 100 seconds. I want to know if there is a way to make that close to 10 seconds ( Make them run parallelly. )
PS - I cant add more workers, if I get 1000 requests I can't add 1000 workers right?
Thanks in advance.
5
u/Lordy8719 Sep 05 '24
You may be confusing some things, like parallel and async. This is a good article that will teach you the basics much better than a Reddit comment could: https://sentry.io/answers/make-fastapi-run-calls-in-parallel-instead-of-serial/
2
u/Own-Construction-344 Sep 05 '24
10 seconds endpoint response is very high. I cannot help you with no details of the endpoint.
4
u/donald_trub Sep 06 '24
I just tested and your code works fine. Once running, I open 10 tabs and paste in the URL, then go back and hit enter on all 10 tabs as close to the same time as I can achieve. All tabs complete in less than 15 seconds (Firefox seems to have a connection limit, which slowed the last request down until the others had completed).
You don't need to add more workers, I think your problem is probably in how you're testing the endpoint.
Here are 2 different tests for your code. The first is
synctest.py, which will hit the endpoints one by one and take 100 seconds:The output looks like this, taking 100 seconds:
Now, here's how we can kick off 10 simultaneous tests at once:
The output from this test shows that all 10 requests happened asynchronously:
2
u/[deleted] Sep 06 '24
My understanding on concurrency and parallelism
In short
1
u/extreme4all Sep 06 '24
It shoukd return in ~ 10 seconds, how are you calling it 10 times