Bird
Raised Fist0
FastAPIframework~10 mins

Lifespan context manager in FastAPI - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Lifespan context manager
Start Application
Enter Lifespan Context
Run Startup Code
Serve Requests
Receive Shutdown Signal
Run Shutdown Code
Exit Lifespan Context
Application Stops
The lifespan context manager runs startup code before serving requests and shutdown code when stopping the app.
Execution Sample
FastAPI
from fastapi import FastAPI
from contextlib import asynccontextmanager

@asynccontextmanager
def lifespan(app: FastAPI):
    print('Starting up')
    yield
    print('Shutting down')

app = FastAPI(lifespan=lifespan)
Defines a lifespan context manager that prints messages on startup and shutdown of the FastAPI app.
Execution Table
StepActionOutputState
1Application startsNo output yetApp created, lifespan context ready
2Enter lifespan contextPrint 'Starting up'Startup code runs
3Yield controlApp ready to serve requestsServing requests
4Receive shutdown signalNo outputPreparing to shutdown
5Exit lifespan contextPrint 'Shutting down'Shutdown code runs
6Application stopsNo outputApp stopped
💡 Application stops after shutdown code runs and lifespan context exits
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5Final
app_stateinitializedstartup completeservingshutdown completestopped
Key Moments - 3 Insights
Why does the startup code run before serving requests?
Because the lifespan context manager runs the code before the yield statement first, as shown in step 2 of the execution table.
When does the shutdown code run?
The shutdown code runs after the app receives a shutdown signal and the lifespan context exits, as shown in step 5.
What happens between the yield and the shutdown code?
The app serves requests during this time, indicated by the state 'serving' after step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the app state after step 3?
Astartup complete
Bserving
Cshutdown complete
Dinitialized
💡 Hint
Check the 'State' column for step 3 in the execution table.
At which step does the shutdown code print 'Shutting down'?
AStep 2
BStep 3
CStep 5
DStep 6
💡 Hint
Look for the 'Print "Shutting down"' output in the execution table.
If the yield statement was removed, what would happen?
AStartup and shutdown code run immediately, no serving requests
BApp serves requests normally
CShutdown code never runs
DApp crashes on start
💡 Hint
Yield pauses startup to serve requests; without it, code runs all at once.
Concept Snapshot
Lifespan context manager in FastAPI:
- Use @asynccontextmanager to define startup and shutdown code.
- Code before yield runs on startup.
- Code after yield runs on shutdown.
- FastAPI app uses lifespan= parameter.
- Controls app lifecycle cleanly.
Full Transcript
The lifespan context manager in FastAPI controls what happens when the app starts and stops. When the app starts, it enters the lifespan context and runs the startup code before serving any requests. This is shown by the print statement 'Starting up'. Then the app serves requests while the code after yield is paused. When the app receives a shutdown signal, it exits the lifespan context and runs the shutdown code, printing 'Shutting down'. This cleanly manages resources and setup/teardown logic. The execution table shows each step and the app state changes from initialized to serving to stopped. The variable tracker follows the app state through these steps. Understanding this flow helps beginners see how FastAPI manages app lifecycle with the lifespan context manager.

Practice

(1/5)
1. What is the main purpose of the lifespan context manager in a FastAPI application?
easy
A. To manage user authentication and authorization
B. To handle HTTP requests and responses
C. To define API routes and endpoints
D. To run setup code when the app starts and cleanup code when it stops

Solution

  1. Step 1: Understand the role of lifespan context manager

    The lifespan context manager is designed to run code at the start and end of the FastAPI app lifecycle.
  2. Step 2: Identify its main use

    It is used to set up resources like database connections when the app starts and clean them up when the app stops.
  3. Final Answer:

    To run setup code when the app starts and cleanup code when it stops -> Option D
  4. Quick Check:

    Lifespan manages startup and shutdown code = A [OK]
Hint: Lifespan runs code at app start and stop [OK]
Common Mistakes:
  • Confusing lifespan with route handling
  • Thinking lifespan manages HTTP requests
  • Assuming lifespan handles user sessions
2. Which of the following is the correct way to define a lifespan context manager in FastAPI?
easy
A. async def lifespan(app): yield
B. def lifespan(): return app
C. async def lifespan(): return app
D. def lifespan(app): yield

Solution

  1. Step 1: Recall lifespan signature

    The lifespan function must be async and accept the app parameter to manage startup and shutdown.
  2. Step 2: Confirm use of yield

    Using yield inside the async function allows running code before and after the yield for startup and shutdown.
  3. Final Answer:

    async def lifespan(app): yield -> Option A
  4. Quick Check:

    Async + app param + yield = A [OK]
Hint: Lifespan is async with app param and uses yield [OK]
Common Mistakes:
  • Forgetting async keyword
  • Missing app parameter
  • Using return instead of yield
3. Given this FastAPI lifespan code snippet, what will be printed when the app starts and stops?
async def lifespan(app):
    print('Starting app')
    yield
    print('Stopping app')
medium
A. Only 'Starting app' is printed
B. 'Starting app' prints on start, 'Stopping app' prints on shutdown
C. Only 'Stopping app' is printed
D. Neither message is printed

Solution

  1. Step 1: Understand yield in lifespan

    The code before yield runs at startup, and code after yield runs at shutdown.
  2. Step 2: Match prints to lifecycle events

    So 'Starting app' prints when app starts, and 'Stopping app' prints when app stops.
  3. Final Answer:

    'Starting app' prints on start, 'Stopping app' prints on shutdown -> Option B
  4. Quick Check:

    Code before yield = start, after yield = stop [OK]
Hint: Code before yield runs on start, after yield on stop [OK]
Common Mistakes:
  • Thinking both prints run immediately
  • Assuming yield blocks all prints
  • Confusing start and stop timing
4. What is wrong with this lifespan context manager code?
async def lifespan(app):
    print('Starting')
    return
    print('Stopping')
medium
A. The app parameter is not used, causing runtime error
B. Missing async keyword causes syntax error
C. Using return instead of yield prevents shutdown code from running
D. Print statements are not allowed in lifespan functions

Solution

  1. Step 1: Identify use of return instead of yield

    The lifespan function must use yield to separate startup and shutdown code.
  2. Step 2: Understand effect of return

    Using return exits the function immediately, so shutdown code after it never runs.
  3. Final Answer:

    Using return instead of yield prevents shutdown code from running -> Option C
  4. Quick Check:

    Return exits early; yield separates start/stop [OK]
Hint: Use yield, not return, to run shutdown code [OK]
Common Mistakes:
  • Confusing return and yield in async functions
  • Ignoring that shutdown code runs after yield
  • Assuming print statements cause errors
5. You want to open a database connection when your FastAPI app starts and close it when the app stops using the lifespan context manager. Which code correctly implements this?
hard
A. async def lifespan(app): db = await connect_db() app.state.db = db yield await db.close()
B. async def lifespan(app): db = await connect_db() yield app.state.db = db await db.close()
C. def lifespan(app): db = connect_db() app.state.db = db yield db.close()
D. async def lifespan(): db = await connect_db() app.state.db = db yield await db.close()

Solution

  1. Step 1: Confirm async function with app parameter

    The lifespan function must be async and accept the app parameter to store the db connection.
  2. Step 2: Check order of operations

    Connect to the database before yield, store it on app.state, then close it after yield.
  3. Step 3: Verify correct use of await and yield

    Await connect_db and db.close, yield separates startup and shutdown code.
  4. Final Answer:

    async def lifespan(app): db = await connect_db() app.state.db = db yield await db.close() -> Option A
  5. Quick Check:

    Async + app param + yield + await connect/close = D [OK]
Hint: Connect before yield, close after, store in app.state [OK]
Common Mistakes:
  • Placing yield before storing db connection
  • Missing async or await keywords
  • Not passing app parameter to lifespan