Run Asynchronous Background Tasks in FastAPI

Background Tasks In FastAPI | Image Created By — Varun Singh
Background Tasks In FastAPI | Image Created By — Varun Singh

FastAPI is a relatively new web framework for Python, taking inspiration from web frameworks like Flask, Django. FastAPI is built on top of Starlette and it brings a ton of awesome features to the table.

It has gained significant traction recently, and after spending the last 6 months working with it every day, I can say that the hype is justified. Today I bring to you, one of the major features FastAPI provides for the developers to run asynchronous tasks right at the API level.

Using BackgroundTasks you can run long-running processes or tasks or functions that are run after returning the response. This way, your client application is not waiting for the task to be completed. Few scenarios where this can help -

  1. Sending Email notification after API is processed.
  2. Submitting a Job to perform data analytics in the background.

Installing FastAPI

$ pip install fastapi uvicorn

Here we are installing FastAPI and uvicorn that will be used to run the FastAPI server.

Background Tasks

It becomes very much necessary to run long-running processes or tasks in your APIs. These tasks can be as simple as sending email notifications to users. Such background tasks may take time to complete and as any best practice would specify that -

API that takes more than 5 seconds time to return a valid response is not a good API

FastAPI provides better control when dealing with background processes. Here I will take the below example to demonstrate how effectively Background Tasks are handled when built with FastAPI -

As an example, I will create an API that sends an email to the end-user and creates an acknowledgment in a custom file, all within one single API.

Lets us simulate Sending an Email using FastAPI’s BackgroundTask feature

First, import BackgroundTasks and define a parameter in your path operation function with a type declaration of :

Importing BackgrounTasks and FastAPI

Create a Task Function

Create a function to be run as the background task. It is a standard function that can receive parameters. It can be an async def or normal def function, FastAPI will know how to handle it correctly. In this case, the task function will write to a file (simulating sending an email).

And as the write operation doesn’t use async and await, we define the function with normal def:

Send Notification API

Registering a Task

Inside the send_notification function, you can now pass the task function to the BackgroundTask’s object with the method add_task().

add_task() receives as arguments:

  • A task function to be run in the background (write_notification).
  • Any sequence of arguments that should be passed to the task function in order (email).
  • Any keyword arguments that should be passed to the task function (message=” some notification”).

Running The Application

We will use uvicorn to run the FastAPI server. Run the following command:

uvicorn main.py:app --reload

The response is returned within 6 ms, which would not have been possible without having our APIs asynchronous. Well, maybe since this is just a simulation — it can be false data. So lets us try adding 10 seconds of sleep in the write_notification function -

Add time.sleep(10)

As shown in the above code, uncomment the line time.sleep(10) and invoke the API again. It will still take time in milliseconds and after 10 seconds have passed a log.txt will be created in the project directory containing the email you send in the API as a parameter.

https://github.com/varun-singh-01/fastapi-background-tasks/tree/master

Conclusion

BackgroundTasks is a FastAPI native way of handling asynchronous tasks that are going to run in the background. One can easily maintain a script of all such tasks and run them as and when required by registering them using the BackgroundTasks object in their APIs.

More About The Author

I am a full-time software engineer with 3.5+ years of experience in Python, AWS, and many more technologies. I have started writing recently as it helps me to read more. I am looking forward to sharing technical knowledge and my life experiences with people out there.

  • Follow me on LinkedIn for more articles and Update — Varun
  • Follow me on Medium — Varun

More content at plainenglish.io

Data Analyst turned Software Developer and Blogger. Enjoys a hot cup of tea and nature’s spell.