From 524a2a8c7bbb613fa391704b795418bc0a7bd62f Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 30 Jun 2022 20:57:19 -0500 Subject: [PATCH] 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. --- app.py | 6 ++++++ gunicorn.conf.py | 2 ++ 2 files changed, 8 insertions(+) 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))