Quiz 2

File Uploads & Background Tasks: File Handling, Celery, Job Processing

327 words
2 min read
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

# File Uploads & Background Tasks: File Handling, Celery, Job Processing ## 🎯 Learning Objectives - Implement secure file upload with validation - Process uploaded files in the background with Celery - Track job status for long-running tasks - Handle concurrent uploads at scale ## 📖 Core Content ### 1.1 Secure Fil...

File Uploads & Background Tasks: File Handling, Celery, Job Processing

🎯 Learning Objectives

  • Implement secure file upload with validation
  • Process uploaded files in the background with Celery
  • Track job status for long-running tasks
  • Handle concurrent uploads at scale

📖 Core Content

1.1 Secure File Upload

python
# runnable
import os
from flask import request, jsonify
from werkzeug.utils import secure_filename
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'pdf'}
UPLOAD_FOLDER = '/app/uploads'
def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/upload', methods=['POST'])
def upload_file():
    if 'file' not in request.files:
        return jsonify({"error": "No file"}), 400
    file = request.files['file']
    if file.filename == '' or not allowed_file(file.filename):
        return jsonify({"error": "Invalid file"}), 400
    # Secure the filename and save
    filename = secure_filename(file.filename)
    filepath = os.path.join(UPLOAD_FOLDER, filename)
    file.save(filepath)
    return jsonify({"message": "File uploaded", "filename": filename})

1.2 Background Processing Flow

(Diagram)

1.3 Why This Matters

File handling is required in most web applications. Background processing prevents the server from hanging during long operations (image resizing, PDF generation). Celery is the standard solution for Python web apps.

2. 📝 Practice Questions

Q1: 100 users simultaneously upload 10MB images. The Flask server times out. Describe the solution using background tasks.
Problem: Flask's synchronous request handling blocks until the file is fully saved. With 100 concurrent uploads, the server runs out of worker threads.
Solution:
  1. Accept upload quickly: Save the file to disk, return 202 Accepted immediately
  2. Queue processing: Enqueue a Celery task for image processing (e.g., resize, thumbnail generation)
  3. Return a task ID: The client receives {"task_id": "abc123"} immediately
  4. Client polls status: The frontend polls /status/abc123 to check progress
  5. Webhook notification (optional): The Celery task sends a webhook when done
This keeps the web server responsive regardless of processing time. Multiple Celery workers process tasks concurrently from the Redis queue. Join Discord Previous7.1 Authentication & AuthorizationNextTesting
Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.