15 lines
460 B
Python
15 lines
460 B
Python
"""Python 3.8 兼容:提供 asyncio.to_thread 的等价实现。"""
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import functools
|
|
from typing import Any, Callable, TypeVar
|
|
|
|
T = TypeVar("T")
|
|
|
|
|
|
async def to_thread(func: Callable[..., T], *args: Any, **kwargs: Any) -> T:
|
|
"""asyncio.to_thread 的 Python 3.8 兼容版本。"""
|
|
loop = asyncio.get_running_loop()
|
|
return await loop.run_in_executor(None, functools.partial(func, *args, **kwargs))
|