gunicorn: fix deadlock when preload + multiple workers was enabled.

Fix Gunicorn hanging on startup when preloading and multiple workers
were enabled. There was some bad interaction between multithreading in
Pytorch and preloading in Gunicorn (possibly because Pytorch can't
survive a fork?). Disabling threading in Pytorch fixes it.
This commit is contained in:
evazion
2022-06-30 20:57:19 -05:00
parent b975102633
commit 524a2a8c7b
2 changed files with 8 additions and 0 deletions
+6
View File
@@ -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
+2
View File
@@ -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))