How to use BackgroundTasks in FastAPI
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints. It's quickly gaining traction in the software development industry for its efficiency, simplicity, and a slew of unique features that allow developers to build robust APIs in record time. In this article, we will focus on one of FastAPI's most powerful features: Background Tasks. Specifically, we'll illustrate how to leverage this feature to perform computations asynchronously while the API stays responsive to other incoming requests.
Code Overview
For this example, let's consider a FastAPI app that handles inference requests. Our API will be accepting an input payload, scheduling a processing task to run in the background, and responding immediately with an offset for that task. The result of the processing task can be retrieved later using this offset. This type of setup is common in scenarios where computation tasks take considerable time, and we don't want to keep the client waiting for the task to complete.
Let's break down the key components of the code:
import logging
import time
from typing import List
import uvicorn
from fastapi import BackgroundTasks, FastAPI, HTTPException
from pydantic import BaseModel, Field
app = FastAPI()…