Flask web server on Raspberry Pi - Time & Space Complexity
When running a Flask web server on a Raspberry Pi, it is important to understand how the server handles requests as more users connect.
We want to know how the time to respond grows when more requests come in.
Analyze the time complexity of the following Flask server code snippet.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return 'Hello from Raspberry Pi!'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
This code starts a simple Flask web server that responds with a greeting message to each request at the home page.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Handling each incoming web request one by one.
- How many times: Once per request, repeated as many times as users connect.
Each new user request causes the server to run the handler function once.
| Input Size (n requests) | Approx. Operations |
|---|---|
| 10 | 10 handler calls |
| 100 | 100 handler calls |
| 1000 | 1000 handler calls |
Pattern observation: The number of operations grows directly with the number of requests.
Time Complexity: O(n)
This means the server work grows linearly with the number of requests it handles.
[X] Wrong: "The server handles all requests instantly no matter how many come in."
[OK] Correct: Each request takes some time to process, so more requests mean more total work and longer total time.
Understanding how a web server handles requests helps you explain real-world system behavior clearly and confidently.
What if the server handled multiple requests at the same time using threads? How would the time complexity change?
Practice
@app.route decorator do in a Flask web server on Raspberry Pi?Solution
Step 1: Understand the role of
This decorator tells Flask which URL path should run the function below it.@app.routeStep 2: Connect URL to function
When a user visits that URL, Flask runs the linked function to generate a response.Final Answer:
It defines the URL path that triggers a specific function. -> Option CQuick Check:
@app.routesets URL path [OK]
- Thinking it starts or stops the server
- Confusing it with installing Flask
- Assuming it runs the server automatically
Solution
Step 1: Understand host parameter in
By default, Flask listens only on localhost (127.0.0.1), meaning only the Pi itself can access it.app.run()Step 2: Use
Setting host to '0.0.0.0' tells Flask to listen on all network interfaces, so other devices can connect.host='0.0.0.0'to allow network accessFinal Answer:
app.run(host='0.0.0.0') -> Option AQuick Check:
host='0.0.0.0' allows network access [OK]
- Using default host which blocks external access
- Confusing port with host
- Using localhost IP which restricts access
http://raspberrypi.local/hello given this Flask code on Raspberry Pi?
from flask import Flask
app = Flask(__name__)
@app.route('/hello')
def hello():
return 'Hello, Raspberry Pi!'
if __name__ == '__main__':
app.run(host='0.0.0.0')Solution
Step 1: Check the route and function
The route '/hello' is defined and returns the string 'Hello, Raspberry Pi!'.Step 2: Accessing the URL
Visiting/hellotriggers the function and returns the string as the web page content.Final Answer:
Hello, Raspberry Pi! -> Option DQuick Check:
Route '/hello' returns greeting string [OK]
- Expecting a 404 error if route is correct
- Thinking code has syntax errors
- Assuming server won't start with host='0.0.0.0'
from flask import Flask
app = Flask(__name__)
@app.route('/status')
def status():
print('Server is running')
if __name__ == '__main__':
app.run(host='0.0.0.0')Solution
Step 1: Check function body indentation
The print statement inside the function must be indented to be part of the function block.Step 2: Verify other parts
Host value '0.0.0.0' is correct, Flask is imported, and route syntax is valid.Final Answer:
Missing indentation for the print statement inside the function. -> Option BQuick Check:
Python requires indentation inside functions [OK]
- Ignoring indentation errors
- Changing host unnecessarily
- Misreading route decorator syntax
/ and /info. Which code correctly sets up these two routes?Solution
Step 1: Check route paths and functions
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return 'Welcome!' @app.route('/info') def info(): return 'Raspberry Pi Info' app.run(host='0.0.0.0') correctly uses '/' for home and '/info' for info, each with its own function returning the right message.Step 2: Verify no route conflicts
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return 'Welcome!' @app.route('/') def info(): return 'Raspberry Pi Info' app.run(host='0.0.0.0') uses '/' twice, which causes conflict. from flask import Flask app = Flask(__name__) @app.route('/info') def home(): return 'Welcome!' @app.route('/') def info(): return 'Raspberry Pi Info' app.run(host='0.0.0.0') swaps routes and functions incorrectly. from flask import Flask app = Flask(__name__) @app.route('/home') def home(): return 'Welcome!' @app.route('/info') def info(): return 'Raspberry Pi Info' app.run(host='0.0.0.0') uses '/home' instead of '/'.Final Answer:
Option A correctly defines two distinct routes '/' and '/info'. -> Option AQuick Check:
Unique routes need unique paths [OK]
- Defining two functions for the same route
- Swapping route paths and function names
- Using wrong URL paths like '/home' instead of '/'
Ready to prove it?
Pass the test and this pattern moves unknown
