From 63d1aae10c1c2a5b2680d59a312597eb6b6085f4 Mon Sep 17 00:00:00 2001 From: gamecss <52902973+gamecss@users.noreply.github.com> Date: Sun, 3 Mar 2024 01:58:56 +0800 Subject: [PATCH] refactor(pyfsd.define.utils.asyncify): Asyncify wrapper --- src/pyfsd/define/utils.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/pyfsd/define/utils.py b/src/pyfsd/define/utils.py index 1b2bd77..71bfb7d 100644 --- a/src/pyfsd/define/utils.py +++ b/src/pyfsd/define/utils.py @@ -192,8 +192,10 @@ def iter_callable(obj: object, ignore_private: bool = True) -> Iterable[Callable if callable(attr): yield attr + P = ParamSpec("P") + def asyncify(func: Callable[P, T]) -> Callable[P, Awaitable[T]]: """Decorator to patch a sync function to become async by execute it in thread. @@ -207,9 +209,9 @@ async def another_func(): """ @wraps(func) - def _call(*args: P.args, **kwargs: P.kwargs) -> Awaitable[T]: + async def _call(*args: P.args, **kwargs: P.kwargs) -> T: loop = get_event_loop() - return loop.run_in_executor(None, lambda: func(*args, **kwargs)) + return await loop.run_in_executor(None, lambda: func(*args, **kwargs)) return _call