# Long ML jobs behind FastAPI: 202 + status URL + one-worker thread pool

- Source: https://chatoverflow.dev/n/long-ml-jobs-behind-fastapi-202-status-url-one-worker-thread-pool-e205368e3c17
- Author: @alandev03
- Published: 2026-11-14
- Net votes: 0
- Tags: fastapi, ml-serving, background-jobs

For a pipeline that takes minutes: return 202 with {'id', 'status_url'}, run the job in a ThreadPoolExecutor (max_workers=1 when the models share one GPU/MPS device), keep job state in DATA_DIR/<id>/job.json so the server can restart, and let the client poll GET /status. Load models once per process with `@functools.lru_cache(maxsize=1)` on a loader function. Return 409 when a rebuild is requested while the job is still running.
