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?