diff --git a/app.py b/app.py index 820264b..a66bb0a 100755 --- a/app.py +++ b/app.py @@ -7,11 +7,17 @@ from base64 import b64encode from fastai.vision.core import PILImage from flask import Flask, request, render_template, jsonify, abort from werkzeug.exceptions import HTTPException +import torch load_dotenv() model_path = getenv("MODEL_PATH", "models/model.pth") autotagger = Autotagger(model_path) +# This is necessary for Gunicorn to work with multiple workers and preloading enabled. +torch.set_num_threads(1) +autotagger.learn.model.eval() +autotagger.learn.model.share_memory() + app = Flask(__name__) app.config["JSON_SORT_KEYS"] = False app.config["JSON_PRETTYPRINT_REGULAR"] = True diff --git a/gunicorn.conf.py b/gunicorn.conf.py index 64a5829..c0cd181 100644 --- a/gunicorn.conf.py +++ b/gunicorn.conf.py @@ -10,3 +10,5 @@ errorlog = getenv("GUNICORN_ERRORLOG", "-") loglevel = getenv("GUNICORN_LOGLEVEL", "info") access_log_format = getenv("GUNICORN_ACCESS_LOG_FORMAT", '{"time":"%(t)s","id":"%({X-Request-Id}i)s","ip":"%(h)s","method":"%(m)s","url":"%(U)s","status":"%(s)s","contentType":"%(Content-Type)s","userAgent":"%(a)s","referer":"%(f)s","sent":"%(B)s","duration":"%(D)s"}') preload_app = bool(strtobool(getenv("GUNICORN_PRELOAD", "True"))) +max_requests = int(getenv("GUNICORN_MAX_REQUESTS", 0)) +max_requests_jitter = int(getenv("GUNICORN_MAX_REQUESTS_JITTER", 0))