Challenge - 5 Problems
FastAPI Concurrency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this FastAPI endpoint using async tasks?
Consider this FastAPI endpoint that runs two async tasks concurrently and returns their results.
What will the response JSON be when calling this endpoint?
What will the response JSON be when calling this endpoint?
FastAPI
from fastapi import FastAPI import asyncio app = FastAPI() async def task1(): await asyncio.sleep(1) return "task1 done" async def task2(): await asyncio.sleep(1) return "task2 done" @app.get("/run-tasks") async def run_tasks(): results = await asyncio.gather(task1(), task2()) return {"results": results}
Attempts:
2 left
💡 Hint
Think about how asyncio.gather collects results in order of the tasks passed.
✗ Incorrect
asyncio.gather runs tasks concurrently but returns results in the order of the input tasks. So task1's result comes first, then task2's.
📝 Syntax
intermediate2:00remaining
Which option correctly runs multiple tasks concurrently in FastAPI?
You want to run three async functions concurrently inside a FastAPI endpoint and wait for all to finish. Which code snippet is correct?
Attempts:
2 left
💡 Hint
Remember that asyncio.gather returns a coroutine that must be awaited.
✗ Incorrect
Option B correctly awaits asyncio.gather which runs all tasks concurrently and returns their results as a list. Option B runs tasks sequentially. Option B misses await so results is a coroutine object. Option B is incorrect usage of asyncio.wait which expects a set of tasks.
🔧 Debug
advanced2:00remaining
Why does this FastAPI endpoint hang indefinitely?
This FastAPI endpoint is supposed to run two async tasks concurrently and return their results. But it never responds. Why?
Code:
from fastapi import FastAPI
import asyncio
app = FastAPI()
async def task1():
await asyncio.sleep(1)
return "done1"
async def task2():
await asyncio.sleep(1)
return "done2"
@app.get("/hang")
def hang():
results = asyncio.gather(task1(), task2())
return {"results": results}
Code:
from fastapi import FastAPI
import asyncio
app = FastAPI()
async def task1():
await asyncio.sleep(1)
return "done1"
async def task2():
await asyncio.sleep(1)
return "done2"
@app.get("/hang")
def hang():
results = asyncio.gather(task1(), task2())
return {"results": results}
FastAPI
from fastapi import FastAPI import asyncio app = FastAPI() async def task1(): await asyncio.sleep(1) return "done1" async def task2(): await asyncio.sleep(1) return "done2" @app.get("/hang") def hang(): results = asyncio.gather(task1(), task2()) return {"results": results}
Attempts:
2 left
💡 Hint
Check if the endpoint function is async and if await is used properly.
✗ Incorrect
The endpoint function is synchronous (def) but calls asyncio.gather without await. This returns a coroutine object instead of running it, so the server never completes the response.
❓ state_output
advanced2:00remaining
What is the output order of concurrent tasks with different sleep times?
Given this FastAPI endpoint, what will be the order of results in the response?
Code:
from fastapi import FastAPI
import asyncio
app = FastAPI()
async def fast_task():
await asyncio.sleep(0.1)
return "fast"
async def slow_task():
await asyncio.sleep(0.5)
return "slow"
@app.get("/order")
async def order():
results = await asyncio.gather(slow_task(), fast_task())
return {"results": results}
Code:
from fastapi import FastAPI
import asyncio
app = FastAPI()
async def fast_task():
await asyncio.sleep(0.1)
return "fast"
async def slow_task():
await asyncio.sleep(0.5)
return "slow"
@app.get("/order")
async def order():
results = await asyncio.gather(slow_task(), fast_task())
return {"results": results}
FastAPI
from fastapi import FastAPI import asyncio app = FastAPI() async def fast_task(): await asyncio.sleep(0.1) return "fast" async def slow_task(): await asyncio.sleep(0.5) return "slow" @app.get("/order") async def order(): results = await asyncio.gather(slow_task(), fast_task()) return {"results": results}
Attempts:
2 left
💡 Hint
asyncio.gather returns results in the order of the input tasks, not completion order.
✗ Incorrect
Even though fast_task finishes first, asyncio.gather returns results in the order tasks were passed. slow_task is first, so its result is first.
🧠 Conceptual
expert2:00remaining
Which statement about FastAPI concurrency with asyncio is TRUE?
Select the correct statement about how FastAPI handles concurrent async tasks using asyncio.
Attempts:
2 left
💡 Hint
Think about how async endpoints and the event loop work in FastAPI.
✗ Incorrect
FastAPI uses the asyncio event loop to run async endpoints concurrently, allowing multiple requests to be processed without blocking. Synchronous blocking code inside async endpoints can block the event loop and reduce concurrency.