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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user