From 531d53381522348c2e6f47a20019b91d5e1b9335 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Tue, 17 Dec 2024 13:50:33 -0400 Subject: [PATCH] feat: optimize _asyncify helper (#494) --- a_sync/a_sync/_helpers.pyx | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/a_sync/a_sync/_helpers.pyx b/a_sync/a_sync/_helpers.pyx index b0b0db06..2283e01e 100644 --- a/a_sync/a_sync/_helpers.pyx +++ b/a_sync/a_sync/_helpers.pyx @@ -53,7 +53,7 @@ cdef object _await(object awaitable): raise -cdef object _asyncify(object func, object executor): # type: ignore [misc] +cdef object _asyncify(object func, executor: Executor): # type: ignore [misc] """ Convert a synchronous function to a coroutine function using an executor. @@ -93,6 +93,16 @@ cdef object _asyncify(object func, object executor): # type: ignore [misc] if iscoroutinefunction(func) or isinstance(func, ASyncFunction): raise exceptions.FunctionNotSync(func) + if hasattr(executor, "run"): + # ASyncExecutor classes are better optimized. + # TODO: implement the same optimizations in the else block below + + run = executor.run + + return wraps(func)( + lambda *args, **kwargs: run(func, *args, **kwargs) + ) + cdef object submit = executor.submit @wraps(func)