0
0
FastAPIframework~20 mins

Event-driven architecture in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Event-Driven Mastery in FastAPI
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when an event is emitted and handled in FastAPI?

Consider this FastAPI code snippet using an event system:

from fastapi import FastAPI
app = FastAPI()

subscribers = []

def on_event(data):
    subscribers.append(data)

@app.post('/emit')
async def emit_event(payload: dict):
    on_event(payload['message'])
    return {'status': 'event received'}

@app.get('/subscribers')
async def get_subscribers():
    return {'messages': subscribers}

What will be the output of a GET request to /subscribers after posting {"message": "hello"} to /emit once?

FastAPI
from fastapi import FastAPI
app = FastAPI()

subscribers = []

def on_event(data):
    subscribers.append(data)

@app.post('/emit')
async def emit_event(payload: dict):
    on_event(payload['message'])
    return {'status': 'event received'}

@app.get('/subscribers')
async def get_subscribers():
    return {'messages': subscribers}
A{"messages": ["hello"]}
B{"messages": []}
C{"messages": ["{'message': 'hello'}"]}
D{"messages": [null]}
Attempts:
2 left
💡 Hint

Think about what the on_event function does with the data it receives.

lifecycle
intermediate
1:30remaining
When does FastAPI's startup event run in an event-driven app?

In FastAPI, you can register a function to run on startup using @app.on_event('startup'). When exactly does this function run?

FastAPI
from fastapi import FastAPI
app = FastAPI()

@app.on_event('startup')
async def startup_event():
    print('App is starting')
AOnly when the first request is received
BEvery time a client sends a request
CWhen the FastAPI server starts before handling any requests
DWhen the server shuts down
Attempts:
2 left
💡 Hint

Think about the server lifecycle phases: startup, running, shutdown.

🔧 Debug
advanced
2:30remaining
Why does this FastAPI event handler not update the shared state?

Look at this FastAPI code:

from fastapi import FastAPI
app = FastAPI()

state = {'count': 0}

def increment():
    state = {'count': state['count'] + 1}

@app.post('/inc')
async def increment_endpoint():
    increment()
    return state

Why does calling /inc never increase count?

FastAPI
from fastapi import FastAPI
app = FastAPI()

state = {'count': 0}

def increment():
    state = {'count': state['count'] + 1}

@app.post('/inc')
async def increment_endpoint():
    increment()
    return state
AFastAPI does not allow modifying global variables inside functions
BThe increment function creates a new local variable 'state' instead of modifying the global one
CThe state dictionary is immutable and cannot be changed
DThe increment function is never called
Attempts:
2 left
💡 Hint

Check variable scope and assignment inside the increment function.

📝 Syntax
advanced
1:30remaining
Which FastAPI event handler code will raise a SyntaxError?

Identify the option that contains a syntax error in defining an event handler in FastAPI.

A
@app.on_event('startup')
async def startup_event()
    print('Starting up')
B
@app.on_event('startup')
async def startup_event():
    print('Starting up')
C
@app.on_event('shutdown')
async def shutdown_event():
    print('Shutting down')
D
@app.on_event('shutdown')
def shutdown_event():
    print('Shutting down')
Attempts:
2 left
💡 Hint

Look carefully at function definitions and punctuation.

🧠 Conceptual
expert
2:00remaining
What is a key benefit of event-driven architecture in FastAPI apps?

Choose the best explanation of why event-driven architecture is useful in FastAPI applications.

AIt forces all code to run synchronously, simplifying debugging
BIt requires fewer lines of code by removing all function definitions
CIt eliminates the need for any shared state or data storage
DIt allows decoupling components so they react to events independently, improving scalability and maintainability
Attempts:
2 left
💡 Hint

Think about how events help parts of an app communicate without tight connections.