📖 Scenario: You are creating a simple web API that returns user data. The API should be fast and able to handle many requests at the same time without waiting. This is important for real-world apps like chat apps or live dashboards.
🎯 Goal: Build a FastAPI app that uses async functions to handle requests. You will create user data, configure a simple setting, write an async route to return users, and complete the app setup to run with ASGI.
📋 What You'll Learn
Create a dictionary called users with exact user data
Add a variable max_users to limit returned users
Write an async function get_users that returns users up to max_users
Complete the FastAPI app setup with correct import and app instance
💡 Why This Matters
🌍 Real World
Many modern web apps need to handle many users at once without slowing down. Using async functions with FastAPI and ASGI lets servers handle many requests efficiently, like chat apps or live data feeds.
💼 Career
Understanding async-first architecture and ASGI is key for backend developers working with Python web frameworks like FastAPI. It helps build fast, scalable APIs used in startups and large companies.
Progress0 / 4 steps
1
Create the user data dictionary
Create a dictionary called users with these exact entries: 1: 'Alice', 2: 'Bob', 3: 'Charlie', 4: 'Diana'.
FastAPI
Hint
Use curly braces to create a dictionary with keys as numbers and values as strings.
2
Add a max_users configuration variable
Add a variable called max_users and set it to 3 to limit how many users the API will return.
FastAPI
Hint
Just create a simple variable with the number 3.
3
Write an async function to get limited users
Write an async function called get_users that returns a dictionary of users limited to max_users. Use async def get_users() and return a dictionary with the first max_users items from users.
FastAPI
Hint
Use dictionary comprehension inside the async function to select only the first max_users users.
4
Complete the FastAPI app setup with async route
Import FastAPI from fastapi, create an app instance called app, and add an async route @app.get('/users') that calls get_users and returns its result.
FastAPI
Hint
Remember to use await when calling the async function inside the route.
Practice
(1/5)
1. What does ASGI enable FastAPI to do compared to traditional synchronous frameworks?
easy
A. Use only synchronous functions for better speed
B. Run only one request at a time to avoid conflicts
C. Handle many requests at the same time using async code
D. Automatically convert Python code to JavaScript
Solution
Step 1: Understand ASGI's role
ASGI is designed to handle multiple requests concurrently using asynchronous programming.
Step 2: Compare with synchronous frameworks
Traditional synchronous frameworks handle one request at a time, blocking others until done.
Final Answer:
Handle many requests at the same time using async code -> Option C
Quick Check:
ASGI = concurrent async handling [OK]
Hint: ASGI means multitasking with async code [OK]
Common Mistakes:
Thinking ASGI runs requests one by one
Confusing ASGI with code conversion tools
Believing async slows down the app
2. Which of the following is the correct way to define an async route handler in FastAPI?
easy
A. async def read_root(): return {"message": "Hello"}
B. def read_root(): return {"message": "Hello"}
C. async read_root(): return {"message": "Hello"}
D. def async read_root(): return {"message": "Hello"}
Solution
Step 1: Recall async function syntax
In Python, async functions start with async def followed by the function name.
Step 2: Check each option's syntax
async def read_root(): return {"message": "Hello"} correctly uses async def. Options C and D have invalid syntax. def read_root(): return {"message": "Hello"} is synchronous.
Final Answer:
async def read_root(): return {"message": "Hello"} -> Option A
Quick Check:
Async function = async def [OK]
Hint: Async functions start with 'async def' [OK]
Common Mistakes:
Omitting 'def' after 'async'
Placing 'async' after 'def'
Using synchronous 'def' for async routes
3. Given this FastAPI route handler, what will be the output when accessed?
A. Missing async keyword before function definition
B. Cannot use await inside async function
C. FastAPI does not support async routes
D. Return statement must be inside try-except block
Solution
Step 1: Check function definition
The function uses await but is defined without async, which is invalid syntax.
Step 2: Understand async function requirements
To use await, the function must be declared with async def.
Final Answer:
Missing async keyword before function definition -> Option A
Quick Check:
Await requires async def [OK]
Hint: Use async def when using await inside function [OK]
Common Mistakes:
Forgetting async before def
Thinking await works in sync functions
Believing FastAPI disallows async routes
5. You want to create a FastAPI endpoint that fetches data from two slow APIs concurrently to improve speed. Which approach best uses FastAPI's async-first architecture?
hard
A. Use async functions with await on both API calls sequentially
B. Use asyncio.gather to run both async API calls concurrently inside an async route
C. Call both APIs one after another using synchronous requests inside an async function
D. Run synchronous API calls in separate threads without async
Solution
Step 1: Understand concurrency with async
Calling APIs sequentially with await waits for each to finish before starting the next, not concurrent.
Step 2: Use asyncio.gather for concurrency
asyncio.gather runs multiple async tasks at the same time, improving speed.
Step 3: Compare options
Use asyncio.gather to run both async API calls concurrently inside an async route uses asyncio.gather inside async route, best leveraging FastAPI's async-first design.
Final Answer:
Use asyncio.gather to run both async API calls concurrently inside an async route -> Option B
Quick Check:
asyncio.gather = concurrent async calls [OK]
Hint: Use asyncio.gather for concurrent async calls [OK]
Common Mistakes:
Awaiting calls sequentially losing concurrency
Using synchronous calls blocking event loop
Ignoring async and threading differences
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.