Skip to content

API process loads onnxruntime/cv2 unnecessarily via worker import chain #163

Description

@Chouffe

Follow-up from #162 (detection-review chunk 1).

Problem

app/main.py imports from app.worker import app as procrastinate_app (needed to open the procrastinate connector in the FastAPI lifespan). app/worker.py in turn does from app.services.smoke_detector import SmokeDetector, and smoke_detector.py imports cv2, onnxruntime, and numpy at module top.

Net effect: the API (uvicorn) process loads onnxruntime + cv2 at startup even though it never runs inference — only the worker process does. This costs ~100 MB RAM and slower startup on the API container for zero benefit.

Suggested fix

Split the lightweight procrastinate App handle out of the heavy task module:

  • app/queue.py (or similar): just app = App(connector=PsycopgConnector(...)) — no heavy imports.
  • main.py and the enqueue endpoint import the App / task from there.
  • Keep the SmokeDetector import (and the @app.task body that uses it) in the worker task module, which only the procrastinate worker process loads.

This keeps onnxruntime/cv2 out of the API process while preserving the "imports at top" convention (no lazy/inline imports).

Notes

  • Not a correctness bug — purely a resource/startup-time cost.
  • ~15-line refactor touching module structure.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions