0

I created a simple FastAPI app. Sometimes the API call works, but other times it gives a random error message.

Here is a minimum reproducible code:

from fastapi import FastAPI
import uvicorn
import time
from loguru import logger
import sys

logger.add("file_{time}.log")
logger.add(sys.stdout, colorize=True, format="<green>{time}</green> <level>{message}</level>")

app = FastAPI()

@app.get("/")
async def root():
    logger.info("starting SLEEP loop")
    for i in range(0, 63):
        time.sleep(10)
        logger.info(f"slept for {(i+1)*10} seconds or {float((i+1)*10/60)} minutes")

    return {"message": "Hello World"}

uvicorn.run(app)

Here is the error it gives randomly:

enter image description here

How to solve this? I suspect this has got something to do with the 10+ minutes I am running this for.

raghavsikaria
  • 867
  • 17
  • 30
  • 2
    REST handlers should really be executing in milliseconds, seconds only if you have to. If a REST handler is taking minutes then that is very bad. You're probably running into a gateway timeout or keep-alive rules killing the connection. Consider different technology for long running asynchronous operations. – flakes Oct 02 '22 at 17:56
  • Yup I understand that, I want to find out why exactly this combination of FastAPI and Uvicorn is not working and how to combat it. – raghavsikaria Oct 02 '22 at 17:58

0 Answers0