0
0
FastAPIframework~10 mins

Concurrent task execution 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 module needed for running tasks concurrently in FastAPI.

FastAPI
from fastapi import FastAPI
import [1]

app = FastAPI()
Drag options to blanks, or click blank then click option'
Ajson
Brequests
Ctime
Dasyncio
Attempts:
3 left
💡 Hint
Common Mistakes
Importing synchronous modules like requests instead of asyncio.
Forgetting to import any concurrency module.
2fill in blank
medium

Complete the code to define an async endpoint that runs two tasks concurrently using asyncio.

FastAPI
import asyncio
from fastapi import FastAPI

app = FastAPI()

async def task1():
    await asyncio.sleep(1)
    return "Task 1 done"

async def task2():
    await asyncio.sleep(1)
    return "Task 2 done"

@app.get("/run-tasks")
async def run_tasks():
    results = await asyncio.[1](task1(), task2())
    return {"results": results}
Drag options to blanks, or click blank then click option'
Await
Bgather
Crun
Dsleep
Attempts:
3 left
💡 Hint
Common Mistakes
Using asyncio.sleep which just pauses execution.
Using asyncio.wait which returns futures, not results directly.
3fill in blank
hard

Fix the error in the code to correctly run two async tasks concurrently and return their results.

FastAPI
import asyncio
from fastapi import FastAPI

app = FastAPI()

async def task1():
    await asyncio.sleep(1)
    return "Done 1"

async def task2():
    await asyncio.sleep(1)
    return "Done 2"

@app.get("/tasks")
async def tasks():
    results = await asyncio.[1](task1(), task2())
    return {"results": results}
Drag options to blanks, or click blank then click option'
Agather
Brun
Cwait
Dcreate_task
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put await before asyncio.gather.
Using asyncio.run inside an async function.
4fill in blank
hard

Fill both blanks to create a background task in FastAPI that runs concurrently without blocking the response.

FastAPI
from fastapi import FastAPI, BackgroundTasks

app = FastAPI()

def write_log(message: str):
    with open("log.txt", "a") as f:
        f.write(message + "\n")

@app.post("/send-message")
async def send_message(message: str, background_tasks: [1]):
    background_tasks.[2](write_log, message)
    return {"message": "Message received"}
Drag options to blanks, or click blank then click option'
ABackgroundTasks
Bbackground_tasks
Cadd_task
Drun_task
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter type like background_tasks lowercase.
Calling a non-existent method like run_task.
5fill in blank
hard

Fill all three blanks to create a FastAPI endpoint that runs multiple async tasks concurrently and returns their results as a dictionary.

FastAPI
import asyncio
from fastapi import FastAPI

app = FastAPI()

async def fetch_data1():
    await asyncio.sleep(1)
    return "Data 1"

async def fetch_data2():
    await asyncio.sleep(1)
    return "Data 2"

@app.get("/fetch-all")
async def fetch_all():
    results = await asyncio.[1](fetch_data1(), fetch_data2())
    return {"first": results[[2]], "second": results[[3]]}
Drag options to blanks, or click blank then click option'
Agather
B0
C1
Dwait
Attempts:
3 left
💡 Hint
Common Mistakes
Using asyncio.wait which returns futures, not results directly.
Mixing up the indexes for results.