Why OCR and Scraping Jobs Run in Celery, Not the Request Thread

Crawling a source site, OCR-ing a scanned page, and translating a poem line by line all take seconds to minutes. None of that belongs between a request and its response, so it runs on a Celery worker behind Redis.

Why OCR and Scraping Jobs Run in Celery, Not the Request Thread

Nothing in this pipeline is fast. Rasterizing a PDF page at 400 DPI for OCR, running Tesseract twice against two page-segmentation modes, translating a poem line by line through argos-translate — each of those is seconds of work on its own, and a full crawl of one source site is many of them in sequence. None of it can sit inside an HTTP request/response cycle without either timing out or making an editor stare at a spinner.

Slow work moves off the request thread
Slow work moves off the request thread

Django enqueues and returns immediately; Redis is the broker; celery_worker does the actual OCR and translation.

The shape of the split

docker-compose.yml runs three containers off the same image and codebase, distinguished only by their command:

web:
  command: gunicorn qasida_app.wsgi:application --bind 0.0.0.0:8000
celery_worker:
  command: celery -A qasida_app worker -l info
celery_beat:
  command: celery -A qasida_app beat -l info

web handles requests and enqueues tasks; it never touches Tesseract, PyMuPDF's rasterizer, or argos-translate's models directly on that path. celery_worker is what actually runs enrich_qasida (the task that derives transliteration and translation for one record) and run_crawlers (the periodic task that walks every active SourceWebsite). celery_beat schedules the periodic ones; nothing else needs to know a schedule exists.

Why a form submission can't just call this synchronously

The enrich_qasida task's own docstring states the reasoning plainly:

@shared_task
def enrich_qasida(qasida_id, overwrite=False):
    """
    Derive transliteration and translation for one qasida.

    Runs on the worker because translating a poem line by line takes seconds,
    which is too slow to hold an admin form open for.
    """

"Too slow to hold an admin form open for" is a concrete, testable bar — not "this feels slow," but a specific claim about what happens to an editor's browser tab if this ran inline. A gunicorn worker blocked on a translation model for several seconds is also a worker not serving any other request, which turns a single slow enrichment into a site-wide latency spike under any real concurrent load.

Redis as broker and result backend, doing one honest job

environment:
  - CELERY_BROKER_URL=redis://127.0.0.1:6379/0
  - CELERY_RESULT_BACKEND=redis://127.0.0.1:6379/0

Redis here isn't a cache in front of Postgres or a session store — it's purely the queue Celery uses to hand tasks to workers and store their results. That's a narrow, well-understood role, which matters because it means Redis going down has a contained blast radius: crawling and enrichment stop being enqueued and processed, but the site itself — reading approved qasidas, browsing, searching — never touches Redis and keeps serving.

Why crawling in particular has to be a background job

run_crawlers() isn't a single fast operation to defer — it's a whole category of slow, unreliable, externally-paced work: politely rate-limited HTTP requests to five different sites, PDF downloads, OCR fallbacks when a text layer turns out to be shattered, image downloads for scans. A single run can legitimately take minutes, and it has to tolerate a source going down mid-crawl without losing progress on the others:

try:
    scraper(website)
except BotChallenge as e:
    print(f"Skipping {website.name}: {e}")
except RateLimited as e:
    print(f"Giving up on {website.name} for this run: {e}")

That kind of per-source fault isolation, running on a schedule with no user waiting on the other end of it, is exactly what a periodic Celery task is for — and exactly the kind of workload that would make no sense bolted onto a request handler.

Series: Qasida App.