Background Tasks in FastAPI
Image Edited By - Varun Singh

Background Tasks in FastAPI

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 feature 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 FastAPI server.


Background Tasks

It becomes very much necessary to run long running process or tasks in your APIs. These tasks can be as simple sending email notification to users. Such background tasks may take time to complete and as any best practise 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 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 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 :

No alt text provided for this image


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-Varun-Singh


Registering a Task

Inside 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 FastAPI server. Run following command:

uvicorn main.py:app --reload
No alt text provided for this image

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

No alt text provided for this image

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

You can find the code in my Github Repo - 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 background. One can easily maintain a script of all such tasks and run them as and when required by registering them using 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


要查看或添加评论,请登录

社区洞察

其他会员也浏览了