Open In App

FastAPI – FastAPI Event Handlers

Last Updated : 09 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A Python framework that is used for building APIs is called FastAPI. There are some circumstances when the user needs to identify a certain event that happened in the API. This can be done using event handlers in FastAPI. In this article, we will study the various event handlers available in FastAPI.

What is Event Handler in FastAPI?

In FastAPI, an event handler is a Python function that is executed automatically when specific events occur during the execution of your application. These event handlers allow you to perform custom actions or modify the behavior of your FastAPI application at different stages of its lifecycle. Event handlers are often used to set up database connections, perform cleanup operations, or customize request and response behaviors.

Types of FastAPI Event Handlers

  • Startup
  • Shutdown

FastAPI Startup Event

The Startup is the event triggered whenever the user starts the FastAPI app using the following command:

uvicorn main:app --reload

This event is basically handled to declare the variables that should be up at the start of the app. It uses the on_event function available in FastAPI to judge the event trigger.

Syntax:

@app.on_event(“startup”)

async def function_name():

print(Statement_startup)

Here,

  • function_name: It is the function which you want to call while the startup event is triggered.
  • Statement_startup: It is the statement under the startup function that will be executed.

Example: In this example, we have created a function event_startup, that gets triggered on the start of the FastAPI app and prints the date and time when the app has started.

Python3




from fastapi import FastAPI
import datetime as dt
 
 
app = FastAPI()
 
# Handle the startup event
@app.on_event("startup")
async def event_startup():
   print('FastAPI Server started :', dt.datetime.now())


Output:

ezgifcom-video-to-gif-(8)

FastAPI Shutdown Event

The Shutdown is the event triggered whenever the user closes the FastAPI app using Ctrl and C keyboard buttons together. This event is basically handled to close any indefinite action being performed that should be ended at the close of the app. It uses the on_event function available in FastAPI to judge the event trigger.

Syntax:

@app.on_event(“shutdown”)

async def function_name():

print(Statement_shutdown)

Here,

  • function_name: It is the function which you want to call while the shutdown event is triggered.
  • Statement_shutdown: It is the statement under the shutdown function that will be executed.

Example: In this example, we have created a function event_shutdown, that gets triggered on the close of the FastAPI app and prints the date and time when the app has been shutdown.

Python3




from fastapi import FastAPI
import datetime as dt
 
app = FastAPI()
 
# Handle the startup event
@app.on_event("shutdown")
async def event_shutdown():
   print('FastAPI Server shutdown :', dt.datetime.now())


Output:

Now, press Ctrl+C to see the shutdown event being triggered.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads