From 1d2e9bc2ab20ec9d7b660bb5653f54e11be56d4b Mon Sep 17 00:00:00 2001 From: cwilvx Date: Tue, 4 Mar 2025 10:16:54 +0300 Subject: [PATCH] try: use gunicorn wsgi server --- manage.py | 33 ++++++++++++++++++++++++++++++++- pyproject.toml | 1 + uv.lock | 14 ++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/manage.py b/manage.py index 4d0ca7ca..dc9eafd6 100644 --- a/manage.py +++ b/manage.py @@ -9,6 +9,7 @@ import waitress import bjoern import mimetypes import setproctitle +import gunicorn.app.base from flask_jwt_extended import ( create_access_token, @@ -229,6 +230,28 @@ def print_memory_usage(response: Response): return response +class StandaloneApplication(gunicorn.app.base.BaseApplication): + def __init__(self, app, options=None): + self.options = options or {} + self.application = app + super().__init__() + + def load_config(self): + if not self.cfg: + return + + config = { + key: value + for key, value in self.options.items() + if key in self.cfg.settings and value is not None + } + for key, value in config.items(): + self.cfg.set(key.lower(), value) + + def load(self): + return self.application + + if __name__ == "__main__": load_into_mem() run_swingmusic() @@ -247,4 +270,12 @@ if __name__ == "__main__": # ipv4=True, # ) # app.run(host=host, port=port, debug=False) - bjoern.run(app, host, port) + # bjoern.run(app, host, port) + + options = { + "bind": f"{host}:{port}", + "workers": 1, + "threads": 100, + "timeout": 0, + } + StandaloneApplication(app, options).run() diff --git a/pyproject.toml b/pyproject.toml index dea79281..ef378e78 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,6 +34,7 @@ dependencies = [ "flask-openapi3==3.0.2", "rapidfuzz==3.11.0", "bjoern>=3.2.2", + "gunicorn>=23.0.0", ] [dependency-groups] diff --git a/uv.lock b/uv.lock index bfdc07f2..c88d2f00 100644 --- a/uv.lock +++ b/uv.lock @@ -498,6 +498,18 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/ac/38/08cc303ddddc4b3d7c628c3039a61a3aae36c241ed01393d00c2fd663473/greenlet-3.1.1-cp313-cp313t-musllinux_1_1_x86_64.whl", hash = "sha256:411f015496fec93c1c8cd4e5238da364e1da7a124bcb293f085bf2860c32c6f6", size = 1142112 }, ] +[[package]] +name = "gunicorn" +version = "23.0.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "packaging" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/34/72/9614c465dc206155d93eff0ca20d42e1e35afc533971379482de953521a4/gunicorn-23.0.0.tar.gz", hash = "sha256:f014447a0101dc57e294f6c18ca6b40227a4c90e9bdb586042628030cba004ec", size = 375031 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/cb/7d/6dac2a6e1eba33ee43f318edbed4ff29151a49b5d37f080aad1e6469bca4/gunicorn-23.0.0-py3-none-any.whl", hash = "sha256:ec400d38950de4dfd418cff8328b2c8faed0edb0d517d3394e457c317908ca4d", size = 85029 }, +] + [[package]] name = "idna" version = "3.10" @@ -1220,6 +1232,7 @@ dependencies = [ { name = "flask-cors" }, { name = "flask-jwt-extended" }, { name = "flask-openapi3" }, + { name = "gunicorn" }, { name = "locust" }, { name = "memory-profiler" }, { name = "pendulum" }, @@ -1256,6 +1269,7 @@ requires-dist = [ { name = "flask-cors", specifier = ">=3.0.10" }, { name = "flask-jwt-extended", specifier = ">=4.6.0" }, { name = "flask-openapi3", specifier = "==3.0.2" }, + { name = "gunicorn", specifier = ">=23.0.0" }, { name = "locust", specifier = ">=2.20.1" }, { name = "memory-profiler", specifier = ">=0.61.0" }, { name = "pendulum", specifier = ">=3.0.0" },