Bird
0
0

Which of the following is the correct way to add a background task for file processing in a FastAPI endpoint?

easy📝 Syntax Q12 of 15
FastAPI - File Handling
Which of the following is the correct way to add a background task for file processing in a FastAPI endpoint?
Adef upload(file: UploadFile, background_tasks: BackgroundTasks): process_file(file)
Bdef upload(file: UploadFile): process_file(file) background_tasks.add_task()
Cdef upload(file: UploadFile): background_tasks = BackgroundTasks() process_file(file)
Ddef upload(file: UploadFile, background_tasks: BackgroundTasks): background_tasks.add_task(process_file, file)
Step-by-Step Solution
Solution:
  1. Step 1: Check function parameters

    To use BackgroundTasks, it must be a parameter in the endpoint function.
  2. Step 2: Add the task correctly

    Use background_tasks.add_task(function, args) to schedule the task after response.
  3. Final Answer:

    def upload(file: UploadFile, background_tasks: BackgroundTasks): background_tasks.add_task(process_file, file) -> Option D
  4. Quick Check:

    Use add_task with BackgroundTasks parameter [OK]
Quick Trick: Add tasks using background_tasks.add_task inside endpoint [OK]
Common Mistakes:
MISTAKES
  • Calling process_file directly inside endpoint
  • Not including BackgroundTasks as a parameter
  • Creating BackgroundTasks inside the function without adding tasks

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes