Forums

Where to load up static constants

Hi I'm trying to load up static 500mb caffemodels files in a constant like so:

# ROOT_FOLDER = os.path.abspath(os.path.dirname(__name__))
# AGE_PROTO = os.path.join(BASE_DIR, '/face_detect_api/weights/age.prototxt.txt')
# AGE_MODEL = os.path.join(BASE_DIR, '/face_detect_api/weights/age.caffemodel')
# GENDER_PROTO = os.path.join(BASE_DIR, '/face_detect_api/weights/gender.prototxt.txt')
# GENDER_MODEL = os.path.join(BASE_DIR, '/face_detect_api/weights/gender.caffemodel')
# HAAR_DETECTOR = cv2.CascadeClassifier(face_detector_path)
# AGE_MODEL = cv2.dnn.readNetFromCaffe(AGE_PROTO, AGE_MODEL)
# GENDER_MODEL = cv2.dnn.readNetFromCaffe(GENDER_PROTO, GENDER_MODEL)
# AGE_OUTPUT_INDEXES = np.array([i for i in range(0, 101)])

I want to load these up so I can access them from my app and I want them to only get loaded once when my site is reloaded. I tried adding them in constants.py in my app and it looks like it was getting reloaded often such as when I ran makemigrations, migrate, etc... it was reloading all of these constants. Also I tried adding them to settings.py and same thing it was reloading them. I'd like them to only get instantiated once when I reload my app.

There are ways that you could do that, but I'd recommend against doing it if it takes more than a few seconds to load. That's because your website's code can be stopped at various times; it will be automatically restarted when a request comes it for it, so normally users won't see any downtime. But if your site takes a long time to start up, those first requests after a shutdown can be pretty slow.

So I guess my first question is, how long does it take to load those things up?

It usually doesn't take more than a few seconds. 5 - 10 seconds max, but usually faster.

Maybe consider one of async solutions as discussed in https://help.pythonanywhere.com/pages/AsyncInWebApps/