Bird
0
0

Identify the error in this lifespan context manager code?

medium📝 Debug Q7 of 15
FastAPI - Middleware and Hooks
Identify the error in this lifespan context manager code?
async def lifespan(app: FastAPI):
    print('Start')
    yield 123
    print('Stop')
AMissing await before print causes error
BApp parameter must be optional
CYielding a value other than null is not allowed in lifespan
DPrint statements cannot be inside lifespan
Step-by-Step Solution
Solution:
  1. Step 1: Understand yield value in lifespan

    Lifespan expects yield with no value (null) to separate startup/shutdown.
  2. Step 2: Effect of yielding 123

    Yielding a non-null value is not allowed in FastAPI lifespan.
  3. Final Answer:

    Yielding a value other than null is not allowed in lifespan -> Option C
  4. Quick Check:

    Yield only null in lifespan [OK]
Quick Trick: Lifespan requires `yield` without a value [OK]
Common Mistakes:
MISTAKES
  • Yielding values other than null
  • Forgetting async keyword
  • Misusing app parameter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes