Turbocharge Your FastAPI with asyncio, Running Background Tasks in Parallel with Immediate Response

Developing a responsive and efficient web application is a paramount goal for developers. One common requirement is the ability to execute multiple tasks in parallel while being able to return an immediate response for the tasks that complete first. This blog post will walk you through a practical example using FastAPI to achieve just that.

The Challenge

Suppose you have three time-consuming tasks to run when an endpoint is triggered, but you want to provide an immediate response as soon as the first task completes, while the remaining tasks continue to run in the background.

Enter FastAPI and asyncio

FastAPI, coupled with Python’s asyncio library, provides a robust way to handle such requirements. Below is a simple yet effective approach to tackle this challenge.

The Code

from fastapi import FastAPI

import asyncio

app = FastAPI()

async def first_sub_function():

# Simulate a delay for the first function

await asyncio.sleep(1)

return "First result"

async def second_sub_function():

# Simulate a delay for the second function

await asyncio.sleep(5)

print("second done")

return "Second result"

async def third_sub_function():

# Simulate a delay for the third function

await asyncio.sleep(3)

print("third done")

return "Third result"


async def run_tasks():

# Create a coroutine for the first sub-function

first_task = first_sub_function()

# Launch the second and third sub-functions as background tasks

second_task = asyncio.create_task(second_sub_function())

third_task = asyncio.create_task(third_sub_function())

# Wait for the first task to complete

first_result = await first_task

# Return only the first task's result

return {"result": first_result}

How It Works

  1. Define Async Functions: We define three asynchronous functions, each simulating a delayed task using await asyncio.sleep().
  1. Create a FastAPI App: We initialize a FastAPI app instance, app.
  1. Design Endpoints: The /run-tasks endpoint triggers the execution of our defined tasks.
  1. Execute and Await Tasks:

    • The first task (first_sub_function()) is awaited directly, meaning the endpoint will wait for its completion.

    • The other two tasks (second_sub_function() and third_sub_function()) are started as background tasks using asyncio.create_task().

  1. Immediate Response: The endpoint returns the result of the first task as soon as it’s available, while the remaining tasks continue to run in the background.

Run The Application

To run the FastAPI app, save the code in a file (e.g., main.py) and run the following command:

uvicorn main:app --reload

Author: robot learner
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source robot learner !