fix(async): offload synchronous plugin methods

This commit is contained in:
InfinityPacer
2026-08-23 15:19:15 +08:00
parent 1f64e99a6f
commit 2ac61337b8
6 changed files with 196 additions and 9 deletions
+25
View File
@@ -21,6 +21,31 @@ async def run_in_threadpool(
return await run_sync(context.run, func, *args)
async def run_in_threadpool_to_completion(
func: Callable[..., Any],
*args: Any,
**kwargs: Any,
) -> Any:
"""在线程调用取得终态后传播取消,避免提前释放仍在使用的执行容量。"""
worker_task = asyncio.create_task(run_in_threadpool(func, *args, **kwargs))
cancellation: asyncio.CancelledError | None = None
while not worker_task.done():
try:
await asyncio.wait({worker_task})
except asyncio.CancelledError as error:
cancellation = cancellation or error
continue
try:
result = worker_task.result()
except Exception as error:
if cancellation is not None:
raise cancellation from error
raise
if cancellation is not None:
raise cancellation
return result
def retry(ExceptionToCheck: Any,
tries: int = 3, delay: int = 3, backoff: int = 2, logger: Any = None):
"""