Challenge - 5 Problems
Async FastAPI 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 async FastAPI endpoint?
Consider this FastAPI async endpoint. What will the client receive when calling
/hello?FastAPI
from fastapi import FastAPI import asyncio app = FastAPI() @app.get('/hello') async def say_hello(): await asyncio.sleep(0.1) return {'message': 'Hello, async world!'}
Attempts:
2 left
💡 Hint
Remember async functions can use await to pause without blocking.
✗ Incorrect
The async endpoint uses await asyncio.sleep(0.1) to pause briefly without blocking. It then returns the JSON with the message 'Hello, async world!'.
📝 Syntax
intermediate2:00remaining
Which option correctly defines an async FastAPI path operation?
Select the option that correctly defines an async GET endpoint in FastAPI that returns a JSON with key 'status' and value 'ok'.
Attempts:
2 left
💡 Hint
Only async functions can use await, and await must be used with awaitable objects.
✗ Incorrect
Option A defines an async function that returns a dictionary directly. Options A, B, and D misuse await or have syntax errors.
❓ state_output
advanced2:00remaining
What is the output when calling this async endpoint twice quickly?
Given this FastAPI async endpoint, what will be the output of two quick calls to
/counter?FastAPI
from fastapi import FastAPI import asyncio app = FastAPI() counter = 0 @app.get('/counter') async def get_counter(): global counter await asyncio.sleep(0.05) counter += 1 return {'count': counter}
Attempts:
2 left
💡 Hint
Think about how async functions run and how global variables update.
✗ Incorrect
Each call awaits 0.05 seconds, then increments the global counter. The first call returns 1, the second returns 2.
🔧 Debug
advanced2:00remaining
Which option causes a runtime error in this async FastAPI endpoint?
Identify which option will cause a runtime error when the endpoint
/data is called.FastAPI
from fastapi import FastAPI app = FastAPI() @app.get('/data') async def get_data(): data = {'a': 1, 'b': 2} result = await data.get('a') return {'result': result}
Attempts:
2 left
💡 Hint
Remember await only works with awaitable objects like coroutines.
✗ Incorrect
The code tries to await an integer (data.get('a') returns 1), which is not awaitable, causing a TypeError.
🧠 Conceptual
expert2:00remaining
Why should FastAPI path operations be async when performing I/O?
Select the best explanation why defining FastAPI path operations as async functions is important when they perform I/O operations like database queries or HTTP calls.
Attempts:
2 left
💡 Hint
Think about what happens when a server waits for slow tasks.
✗ Incorrect
Async functions let the server pause on slow I/O without blocking other requests, improving efficiency and user experience.