0
0
FastAPIframework~20 mins

Async vs sync decision in FastAPI - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async vs Sync Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when mixing async and sync endpoints in FastAPI?
Consider a FastAPI app with one async endpoint and one sync endpoint. What happens when both are called concurrently?
FastAPI
from fastapi import FastAPI
import time
import asyncio

app = FastAPI()

@app.get('/sync')
def sync_endpoint():
    time.sleep(1)
    return {'message': 'sync done'}

@app.get('/async')
async def async_endpoint():
    await asyncio.sleep(1)
    return {'message': 'async done'}
ABoth endpoints block independently; sync blocks the server thread, async allows concurrency.
BBoth endpoints run concurrently without blocking each other.
CSync endpoint runs asynchronously, async endpoint runs synchronously.
DBoth endpoints block the server and cause a deadlock.
Attempts:
2 left
💡 Hint
Think about how Python handles time.sleep vs asyncio.sleep in FastAPI.
📝 Syntax
intermediate
2:00remaining
Which FastAPI endpoint definition is correct for async behavior?
Select the correct FastAPI endpoint definition that properly uses async syntax.
A@app.get('/data')\ndef get_data():\n return {'data': await some_async_call()}
B@app.get('/data')\ndef get_data():\n await some_async_call()\n return {'data': 'async'}
C@app.get('/data')\nasync def get_data():\n time.sleep(1)\n return {'data': 'async'}
D@app.get('/data')\nasync def get_data():\n return {'data': 'async'}
Attempts:
2 left
💡 Hint
Remember that 'await' can only be used inside async functions.
state_output
advanced
2:00remaining
What is the output of this FastAPI async endpoint with blocking code?
Analyze the output and behavior of this endpoint when called multiple times concurrently.
FastAPI
from fastapi import FastAPI
import time
import asyncio

app = FastAPI()

@app.get('/block')
async def block_endpoint():
    time.sleep(2)
    return {'status': 'done'}
AThe endpoint raises a RuntimeWarning but returns immediately.
BRequests run concurrently without blocking despite time.sleep usage.
CEach request blocks the event loop for 2 seconds, causing all requests to be processed sequentially.
DThe server crashes due to mixing sync blocking code in async function.
Attempts:
2 left
💡 Hint
Consider what happens when blocking code runs inside an async function.
🔧 Debug
advanced
2:00remaining
Why does this FastAPI async endpoint cause slow responses?
Identify the cause of slow responses in this async endpoint code.
FastAPI
from fastapi import FastAPI
import time
import asyncio

app = FastAPI()

@app.get('/slow')
async def slow_endpoint():
    for _ in range(3):
        time.sleep(1)
    return {'message': 'done'}
AThe for loop is not awaited causing the function to run synchronously.
BUsing time.sleep inside async blocks the event loop causing slow responses.
CMissing await before time.sleep causes the function to hang.
DThe endpoint lacks async keyword causing slow execution.
Attempts:
2 left
💡 Hint
Check which calls block the event loop in async functions.
🧠 Conceptual
expert
2:00remaining
When should you choose sync over async endpoints in FastAPI?
Select the best scenario to use synchronous endpoints instead of asynchronous ones in FastAPI.
AWhen the endpoint performs CPU-bound tasks that do not benefit from async concurrency.
BWhen the endpoint makes many network calls that support async operations.
CWhen you want to maximize concurrency for I/O-bound tasks.
DWhen the endpoint uses async libraries exclusively.
Attempts:
2 left
💡 Hint
Think about the nature of the task: CPU-bound vs I/O-bound.