Performance: Why background processing matters
Background processing affects how fast the main web page loads and how responsive the server feels to users.
from flask import Flask, request from threading import Thread app = Flask(__name__) def background_task(): do_heavy_task() @app.route('/process') def process(): Thread(target=background_task).start() return 'Task started'
from flask import Flask, request app = Flask(__name__) @app.route('/process') def process(): # Long task runs here do_heavy_task() return 'Done'
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous long task in request | Minimal | Minimal | Delayed due to slow response | [X] Bad |
| Background task with immediate response | Minimal | Minimal | Fast initial paint | [OK] Good |
import threading
from flask import Flask
app = Flask(__name__)
def slow_task():
import time
time.sleep(5)
print('Task done')
@app.route('/')
def index():
threading.Thread(target=slow_task).start()
return 'Request received'import threading
from flask import Flask
app = Flask(__name__)
def task():
print('Running task')
@app.route('/')
def index():
thread = threading.Thread(target=task)
thread.run()
return 'Done'Pass the test and this pattern moves unknown