0
0
FastAPIframework~20 mins

Lifespan context manager in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI Lifespan Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens during the lifespan context manager in FastAPI?

Consider a FastAPI app using a lifespan context manager to open and close a database connection. What is the correct sequence of events when the app starts and stops?

FastAPI
from fastapi import FastAPI
from contextlib import asynccontextmanager

@asynccontextmanager
async def lifespan(app: FastAPI):
    print('Connecting to DB')
    yield
    print('Disconnecting from DB')

app = FastAPI(lifespan=lifespan)
A1,2,3
B2,1,3
C1,3,2
D3,1,2
Attempts:
2 left
💡 Hint

Think about what happens before and after the yield statement in a context manager.

state_output
intermediate
2:00remaining
What is printed when the FastAPI app with lifespan starts and stops?

Given this FastAPI app with a lifespan context manager, what will be printed to the console when the app starts and then stops?

FastAPI
from fastapi import FastAPI
from contextlib import asynccontextmanager

@asynccontextmanager
async def lifespan(app: FastAPI):
    print('Starting app')
    yield
    print('Stopping app')

app = FastAPI(lifespan=lifespan)
AStarting app\nStopping app
BStopping app\nStarting app
CStarting app
DStopping app
Attempts:
2 left
💡 Hint

Remember the lifespan context manager runs code before and after the yield.

🔧 Debug
advanced
2:00remaining
Why does this FastAPI lifespan context manager cause an error?

Examine this lifespan context manager code. Why does it raise a RuntimeError when the app starts?

FastAPI
from fastapi import FastAPI
from contextlib import asynccontextmanager

@asynccontextmanager
async def lifespan(app: FastAPI):
    print('Setup')
    # Missing yield statement
    print('Teardown')

app = FastAPI(lifespan=lifespan)
AIt raises RuntimeError because the app variable is not used inside lifespan.
BIt raises RuntimeError because print statements are not allowed in lifespan.
CIt raises RuntimeError because FastAPI requires a synchronous lifespan function.
DIt raises RuntimeError because the async context manager function does not yield.
Attempts:
2 left
💡 Hint

Async context managers must have exactly one yield.

📝 Syntax
advanced
2:00remaining
Which lifespan context manager code is syntactically correct in FastAPI?

Choose the option with correct syntax for an async lifespan context manager in FastAPI.

A
from contextlib import contextmanager

@contextmanager
def lifespan(app: FastAPI):
    print('Start')
    yield
    print('End')
B
from contextlib import asynccontextmanager

def lifespan(app: FastAPI):
    print('Start')
    yield
    print('End')
C
from contextlib import asynccontextmanager

@asynccontextmanager
async def lifespan(app: FastAPI):
    print('Start')
    yield
    print('End')
D
from contextlib import asynccontextmanager

@asynccontextmanager
def lifespan():
    print('Start')
    yield
    print('End')
Attempts:
2 left
💡 Hint

FastAPI expects an async context manager with the app parameter.

🧠 Conceptual
expert
2:00remaining
What is the main benefit of using a lifespan context manager in FastAPI?

Why would a developer use a lifespan context manager in a FastAPI application?

ATo automatically reload the app when code changes are detected.
BTo manage startup and shutdown tasks like opening and closing resources cleanly.
CTo handle HTTP request routing and URL matching.
DTo replace dependency injection for request handlers.
Attempts:
2 left
💡 Hint

Think about what happens when the app starts and stops.