For some reason FastAPI doesn't respond to any requests while a request is handled. I would expect FastAPI to be able to handle multiple requests at the same time. I would like to allow multiple calls at the same time as multiple users might be accessing the REST API.
Minimal Example: Asynchronous Processes
After starting the server: uvicorn minimal:app --reload
, running the request_test run request_test
and executing test()
, I get as expected {'message': 'Done'}
. However, when I execute it again within the 20 second frame of the first request, the request is not being processed until the sleep_async
from the first call is finished.
Without asynchronous Processes
The same problem (that I describe below) exists even if I don't use asynchronous calls and wait directly within async def info. That doesn't make sense to me.
FastAPI: minimal
#!/usr/bin/env python3
from fastapi import FastAPI
from fastapi.responses import JSONResponse
import time
import asyncio
app = FastAPI()
@app.get("/test/info/")
async def info():
async def sleep_async():
time.sleep(20)
print("Task completed!")
asyncio.create_task(sleep_async())
return JSONResponse(content={"message": "Done"})
Test: request_test
#!/usr/bin/env python3
import requests
def test():
print("Before")
response = requests.get(f"http://localhost:8000/test/info")
print("After")
response_data = response.json()
print(response_data)