mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-08-28 19:47:41 +08:00
refactor: unify protected task completion
This commit is contained in:
@@ -3,12 +3,29 @@ import inspect
|
||||
import time
|
||||
from contextvars import copy_context
|
||||
from functools import partial, wraps
|
||||
from typing import Any, Callable
|
||||
from typing import Any, Callable, TypeVar
|
||||
|
||||
from app.schemas.exception import ImmediateException
|
||||
from anyio.to_thread import run_sync
|
||||
|
||||
|
||||
TaskResult = TypeVar("TaskResult")
|
||||
|
||||
|
||||
async def await_task_to_terminal(
|
||||
task: asyncio.Future[TaskResult],
|
||||
) -> TaskResult:
|
||||
"""忽略当前调用方的重复取消,直到受保护任务进入真实终态。"""
|
||||
while not task.done():
|
||||
try:
|
||||
await asyncio.shield(task)
|
||||
except asyncio.CancelledError:
|
||||
continue
|
||||
except BaseException:
|
||||
break
|
||||
return task.result()
|
||||
|
||||
|
||||
async def run_in_threadpool(
|
||||
func: Callable[..., Any],
|
||||
*args: Any,
|
||||
|
||||
Reference in New Issue
Block a user