Skip to content

Commit 8818d08

Browse files
fix(t2i): run Shiki runtime injection in executor to avoid blocking event loop
1 parent 0830f48 commit 8818d08

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

astrbot/core/utils/t2i/network_strategy.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,12 @@ async def render_custom_template(
156156
if options:
157157
default_options |= options
158158

159-
if SHIKI_RUNTIME_TEMPLATE_PATTERN.search(tmpl_str):
160-
tmpl_data = {"shiki_runtime": get_shiki_runtime()} | tmpl_data
161-
tmpl_str = inject_shiki_runtime(tmpl_str)
159+
# 在线程池中执行 Shiki 注入,避免 1.2MB JS 处理阻塞事件循环
160+
import asyncio
161+
loop = asyncio.get_event_loop()
162+
tmpl_str, tmpl_data = await loop.run_in_executor(
163+
None, self._prepare_template_sync, tmpl_str, tmpl_data
164+
)
162165
post_data = {
163166
"tmpl": tmpl_str,
164167
"json": return_url,
@@ -219,3 +222,12 @@ async def render(
219222
},
220223
return_url,
221224
)
225+
226+
def _prepare_template_sync(
227+
self, tmpl_str: str, tmpl_data: dict
228+
) -> tuple[str, dict]:
229+
"""在线程池中执行的同步模板预处理(避免阻塞事件循环)"""
230+
if SHIKI_RUNTIME_TEMPLATE_PATTERN.search(tmpl_str):
231+
tmpl_data = {"shiki_runtime": get_shiki_runtime()} | tmpl_data
232+
tmpl_str = inject_shiki_runtime(tmpl_str)
233+
return tmpl_str, tmpl_data

0 commit comments

Comments
 (0)