0
0
FastAPIframework~10 mins

Background file processing in FastAPI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AFastAPI
BRequest
CBackgroundTasks
DResponse
Attempts:
3 left
💡 Hint
Common Mistakes
Importing BackgroundTasks instead of FastAPI
Using Request instead of FastAPI
2fill in blank
medium

Complete 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'
ABackgroundTasks
BRequest
CDepends
DUploadFile
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request or Depends instead
Forgetting to import BackgroundTasks
3fill in blank
hard

Fix 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'
Arequest: Request
Bbg: BackgroundTasks
Ctasks: BackgroundTasks
Dbackground_tasks: BackgroundTasks
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter name
Wrong type annotation
Missing BackgroundTasks import
4fill in blank
hard

Fill 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'
Aadd_task
Bfile
Cprocess_file
Drun_task
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method name
Passing wrong argument to the task
5fill in blank
hard

Fill 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'
Aprocess_file
Bfile
Cadd_task
Dupload_file
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching function names
Wrong method to schedule task
Incorrect parameter usage