From 939e63f244e6c296068a1db07af273a2619473c8 Mon Sep 17 00:00:00 2001 From: retoor Date: Mon, 8 Sep 2025 00:59:11 +0200 Subject: [PATCH] Executor pools. --- src/snek/service/channel_message.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/snek/service/channel_message.py b/src/snek/service/channel_message.py index 6c1ae62..7f21282 100644 --- a/src/snek/service/channel_message.py +++ b/src/snek/service/channel_message.py @@ -4,7 +4,6 @@ import time import asyncio from concurrent.futures import ThreadPoolExecutor -executor = ThreadPoolExecutor(max_workers=50) class ChannelMessageService(BaseService): @@ -13,6 +12,12 @@ class ChannelMessageService(BaseService): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._configured_indexes = False + self._executor_pools = {} + + def get_or_create_executor(self, user_uid): + if not user_uid in self._executor_pools: + self._executor_pools[user_uid] = ThreadPoolExecutor(max_workers=5) + return self._executor_pools[user_uid] async def maintenance(self): args = {} @@ -77,8 +82,8 @@ class ChannelMessageService(BaseService): loop = asyncio.get_event_loop() try: template = self.app.jinja2_env.get_template("message.html") - model["html"] = await loop.run_in_executor(executor, lambda: template.render(**context)) - model['html'] = await loop.run_in_executor(executor, lambda:sanitize_html(model['html'])) + model["html"] = await loop.run_in_executor(self.get_or_create_executor(user["uid"]), lambda: template.render(**context)) + model['html'] = await loop.run_in_executor(self.get_or_create_executor(user["uid"]), lambda:sanitize_html(model['html'])) except Exception as ex: print(ex, flush=True) @@ -136,8 +141,8 @@ class ChannelMessageService(BaseService): template = self.app.jinja2_env.get_template("message.html") loop = asyncio.get_event_loop() - model["html"] = await loop.run_in_executor(executor, lambda: template.render(**context)) - model['html'] = await loop.run_in_executor(executor, lambda: sanitize_html(model['html'])) + model["html"] = await loop.run_in_executor(self.get_or_create_executor(user["uid"]), lambda: template.render(**context)) + model['html'] = await loop.run_in_executor(self.get_or_create_executor(user["uid"]), lambda: sanitize_html(model['html'])) return await super().save(model) async def offset(self, channel_uid, page=0, timestamp=None, page_size=30):