Complete the code to import the correct FastAPI class.
from fastapi import [1] app = [1]()
The FastAPI class is imported to create the app instance.
Complete the code to import BackgroundTasks for running tasks in background.
from fastapi import FastAPI, [1]
BackgroundTasks is imported to schedule background jobs.
Fix the error in the endpoint to accept BackgroundTasks as a parameter.
@app.post('/upload') async def upload_file(file: bytes, [1]): background_tasks.add_task(process_file, file) return {'message': 'File received'}
The parameter must be named and typed as background_tasks: BackgroundTasks to use FastAPI's background task system.
Fill both blanks to schedule the background task correctly.
async def upload(file: bytes, background_tasks: BackgroundTasks): background_tasks.[1](process_file, [2]) return {'status': 'processing'}
Use add_task method to schedule process_file with file as argument.
Fill all three blanks to define a background task function and schedule it.
def [1](file: bytes): # simulate processing print(f'Processing file of size: {len([2])} bytes') @app.post('/upload') async def upload(file: bytes, background_tasks: BackgroundTasks): background_tasks.[3]([1], file) return {'message': 'File is being processed'}
The function is named process_file, it uses the file parameter, and add_task schedules it.
