Performance: How Flask processes HTTP requests
This concept affects the server response time and how quickly the browser receives the page content.
from flask import Flask import threading app = Flask(__name__) @app.route('/') def index(): def background_task(): # Simulate slow processing without blocking import time time.sleep(5) threading.Thread(target=background_task).start() return 'Hello World!' if __name__ == '__main__': app.run()
from flask import Flask app = Flask(__name__) @app.route('/') def index(): import time time.sleep(5) # Simulate slow processing return 'Hello World!' if __name__ == '__main__': app.run()
| Pattern | Server Processing | Response Delay | User Impact | Verdict |
|---|---|---|---|---|
| Blocking long tasks in request handler | High CPU wait | High delay (seconds) | Slow page load, poor LCP | [X] Bad |
| Background task for slow operations | Low CPU wait in handler | Minimal delay | Fast page load, good LCP | [OK] Good |
from flask import Flask, request
app = Flask(__name__)
@app.route('/greet', methods=['POST'])
def greet():
name = request.form.get('name', 'Guest')
return f'Hello, {name}!'@app.route('/data', methods=['GET', 'POST'])
def data():
if request.method == 'POST'
return 'Posted data'
else:
return 'Get data'Pass the test and this pattern moves unknown