Skip to content

Commit f9e890a

Browse files
committed
Guard TransformerModel preload to runserver processes only
1 parent dec3c12 commit f9e890a

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

server/api/apps.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,28 @@ class ApiConfig(AppConfig):
66
name = 'api'
77

88
def ready(self):
9+
import os
10+
import sys
11+
12+
# ready() runs in every Django process: migrate, test, shell, runserver, etc.
13+
# Only preload the model when we're actually going to serve requests.
14+
# Dev (docker-compose.yml) runs `manage.py runserver 0.0.0.0:8000`.
15+
# Prod (Dockerfile.prod CMD) runs `manage.py runserver 0.0.0.0:8000 --noreload`.
16+
# entrypoint.prod.sh also runs migrate, createsu, and populatedb before exec'ing
17+
# runserver — the guard below correctly skips model loading for those commands too.
18+
if sys.argv[1:2] != ['runserver']:
19+
return
20+
21+
# Dev's autoreloader spawns two processes: a parent file-watcher and a child
22+
# server. ready() runs in both, but only the child (RUN_MAIN=true) serves
23+
# requests. Skip the parent to avoid loading the model twice on each file change.
24+
# Prod uses --noreload so RUN_MAIN is never set; 'noreload' in sys.argv handles that case.
25+
if os.environ.get('RUN_MAIN') != 'true' and '--noreload' not in sys.argv:
26+
return
27+
28+
# Note: paraphrase-MiniLM-L6-v2 (~80MB) is downloaded from HuggingFace on first
29+
# use and cached to ~/.cache/torch/sentence_transformers/ inside the container.
30+
# That cache is ephemeral — every container rebuild re-downloads the model unless
31+
# a volume is mounted at that path.
932
from .services.sentencetTransformer_model import TransformerModel
1033
TransformerModel.get_instance()

0 commit comments

Comments
 (0)