Skip to content

Commit

Permalink
refactor(pyfsd.define.utils.asyncify): Asyncify wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
gamecss committed Mar 2, 2024
1 parent 2bc1246 commit 63d1aae
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/pyfsd/define/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand Down

0 comments on commit 63d1aae

Please sign in to comment.