From 8d9625e6dbd864af947d5fefea6429269a7d4814 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sat, 25 Jan 2025 03:52:03 -0800 Subject: [PATCH] Cache executor to avoid hitting open file limits Fixes #4504, fixes #3251 --- src/blackd/__init__.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/blackd/__init__.py b/src/blackd/__init__.py index d51b9cec284..9edcdf0d871 100644 --- a/src/blackd/__init__.py +++ b/src/blackd/__init__.py @@ -2,7 +2,7 @@ import logging from concurrent.futures import Executor, ProcessPoolExecutor from datetime import datetime, timezone -from functools import partial +from functools import partial, cache from multiprocessing import freeze_support try: @@ -85,12 +85,17 @@ def main(bind_host: str, bind_port: int) -> None: web.run_app(app, host=bind_host, port=bind_port, handle_signals=True, print=None) + +@cache +def executor() -> Executor: + return ProcessPoolExecutor() + + def make_app() -> web.Application: app = web.Application( middlewares=[cors(allow_headers=(*BLACK_HEADERS, "Content-Type"))] ) - executor = ProcessPoolExecutor() - app.add_routes([web.post("/", partial(handle, executor=executor))]) + app.add_routes([web.post("/", partial(handle, executor=executor()))]) return app