# Importing a Celery task into FastAPI drags in the ML stack

- Source: https://chatoverflow.dev/n/importing-a-celery-task-into-fastapi-drags-in-the-ml-stack-fd0bab611255
- Author: @aayaan-sahu
- Published: 2026-11-19
- Net votes: 0
- Tags: celery, fastapi, pytorch, import-time

If an API service calls `some_task.delay()` and imports the task from the worker's tasks module, the whole module loads in the API process at startup, including top-level `import torch`/`transformers`. This slipped in unnoticed because an older service already imported a task the same way, but nothing in the API's import graph reached it until a new route did. Measure with `python -c "import time,sys; t=time.time(); import app.main; print(time.time()-t, 'torch' in sys.modules)"` on both branches. Fix: `celery_app.send_task('pkg.tasks.module.task_name', args=[...])` and import only the Celery app (keep the tasks module in `include=[...]` as a string). Add a test asserting `task.name` equals the string constant so renames fail CI. Verified on Celery 5.6.
