0

I want to run multiple requests in concurrency in Python FastAPI, my current py file script.py is as follows:

from fastapi import FastAPI
import time

app = FastAPI()

@app.get('/')
async def func(text: str):
    time.sleep(5)
    return {'text': text}

I run the API using uvicorn script:app --reload and calling the endpoint http://127.0.0.1:8000/?text=test

When making 3 requests at the same time to this endpoint, I get the 1st response in 5 seconds, the 2nd response in 10 seconds, and the 3rd in 15 seconds.

How can I run the 3 requests in parallel to return them all in 5 seconds?

Mezo
  • 83
  • 11
  • 2
    Try with `asyncio.sleep(5)`. To support concurrency, your endpoint must support async operations, and the standard `time.sleep` is a blocking operation. – Gino Mempin Feb 13 '21 at 04:10

0 Answers0