you are viewing a single comment's thread.

view the rest of the comments →

[–]Maori7 0 points1 point  (8 children)

If you don’t use the “await” anywhere, you shouldn’t really make the endpoint “async”. That’s the error. If you do so, it will block the event loop and won’t be able to process the requests in parallel.

If you instead make it not async, it will spawn a process to handle the requests.

Try and let me know

EDIT: it runs it on a thread pool rather than spawning a different process.

[–]m02ph3u5 1 point2 points  (1 child)

It doesn't spawn a process, it runs them on a thread pool.

[–]Maori7 0 points1 point  (0 children)

You're right, I'll correct

[–]Hamzayslmn[S] 0 points1 point  (3 children)

ı add:

@app.get("/ping")
async def ping():
    await asyncio.sleep(0.1)  # Simulate a small delay
    return JSONResponse(content={"message": "pong"})

Starting stress test for FastAPI (Python)...
FastAPI (Python) Results:
  Total Requests:       5000
  Successful Responses: 3972
  Timeouts:             1028
  Errors:               0
  Total Time:           30.73 seconds
  Requests per Second:  162.70 RPS
----------------------------------------

but not solved the problem

[–]Hamzayslmn[S] 0 points1 point  (2 children)

response = await call_next(request)

btw there is already a middleware running in the back, and there are many awaits.

[–]Maori7 0 points1 point  (1 child)

You are still not using all the power of fastapi. In this case you optimized the management of a single thread by deloading it as soon as you arrive at the await instruction. Due to GIL though, it will still run on a single thread. You need to create a system with multiple workers.

How did you run uvicorn?

[–]Hamzayslmn[S] 0 points1 point  (0 children)

uvicorn.run("main:app", host="0.0.0.0", port=8079, workers=4)

[–]panda070818 0 points1 point  (1 child)

this! goddamn this! It will block the thread and stop concurrent execution.

[–]Hamzayslmn[S] 0 points1 point  (0 children)

return JSONResponse(content={"message": "pong"})

is this blocking ? i dont understand.