Executor pools.
This commit is contained in:
parent
b4c267d584
commit
939e63f244
@ -4,7 +4,6 @@ import time
|
|||||||
import asyncio
|
import asyncio
|
||||||
from concurrent.futures import ThreadPoolExecutor
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
|
||||||
executor = ThreadPoolExecutor(max_workers=50)
|
|
||||||
|
|
||||||
|
|
||||||
class ChannelMessageService(BaseService):
|
class ChannelMessageService(BaseService):
|
||||||
@ -13,6 +12,12 @@ class ChannelMessageService(BaseService):
|
|||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self._configured_indexes = False
|
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):
|
async def maintenance(self):
|
||||||
args = {}
|
args = {}
|
||||||
@ -77,8 +82,8 @@ class ChannelMessageService(BaseService):
|
|||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
try:
|
try:
|
||||||
template = self.app.jinja2_env.get_template("message.html")
|
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(self.get_or_create_executor(user["uid"]), 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:sanitize_html(model['html']))
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
print(ex, flush=True)
|
print(ex, flush=True)
|
||||||
|
|
||||||
@ -136,8 +141,8 @@ class ChannelMessageService(BaseService):
|
|||||||
template = self.app.jinja2_env.get_template("message.html")
|
template = self.app.jinja2_env.get_template("message.html")
|
||||||
|
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
model["html"] = await loop.run_in_executor(executor, lambda: template.render(**context))
|
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(executor, lambda: sanitize_html(model['html']))
|
model['html'] = await loop.run_in_executor(self.get_or_create_executor(user["uid"]), lambda: sanitize_html(model['html']))
|
||||||
return await super().save(model)
|
return await super().save(model)
|
||||||
|
|
||||||
async def offset(self, channel_uid, page=0, timestamp=None, page_size=30):
|
async def offset(self, channel_uid, page=0, timestamp=None, page_size=30):
|
||||||
|
Loading…
Reference in New Issue
Block a user