Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the correct FastAPI class.
FastAPI
from fastapi import [1] app = [1]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing BackgroundTasks instead of FastAPI
Using Request instead of FastAPI
✗ Incorrect
The FastAPI class is imported to create the app instance.
2fill in blank
mediumComplete the code to import BackgroundTasks for running tasks in background.
FastAPI
from fastapi import FastAPI, [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request or Depends instead
Forgetting to import BackgroundTasks
✗ Incorrect
BackgroundTasks is imported to schedule background jobs.
3fill in blank
hardFix the error in the endpoint to accept BackgroundTasks as a parameter.
FastAPI
@app.post('/upload') async def upload_file(file: bytes, [1]): background_tasks.add_task(process_file, file) return {'message': 'File received'}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter name
Wrong type annotation
Missing BackgroundTasks import
✗ Incorrect
The parameter must be named and typed as background_tasks: BackgroundTasks to use FastAPI's background task system.
4fill in blank
hardFill both blanks to schedule the background task correctly.
FastAPI
async def upload(file: bytes, background_tasks: BackgroundTasks): background_tasks.[1](process_file, [2]) return {'status': 'processing'}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method name
Passing wrong argument to the task
✗ Incorrect
Use add_task method to schedule process_file with file as argument.
5fill in blank
hardFill all three blanks to define a background task function and schedule it.
FastAPI
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'}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching function names
Wrong method to schedule task
Incorrect parameter usage
✗ Incorrect
The function is named process_file, it uses the file parameter, and add_task schedules it.