Skip to content

Commit f9ec466

Browse files
authored
refactor: extract is_courinte_fn as a common util (#1305)
1 parent d205643 commit f9ec466

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

python/cocoindex/runtime.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,14 @@ def run(self, coro: Coroutine[Any, Any, T]) -> T:
6868
execution_context = _ExecutionContext()
6969

7070

71-
def to_async_call(call: Callable[..., Any]) -> Callable[..., Awaitable[Any]]:
72-
if isinstance(call, (staticmethod, classmethod)):
73-
base_call = call.__func__
71+
def is_coroutine_fn(fn: Callable[..., Any]) -> bool:
72+
if isinstance(fn, (staticmethod, classmethod)):
73+
return inspect.iscoroutinefunction(fn.__func__)
7474
else:
75-
base_call = call
76-
if inspect.iscoroutinefunction(base_call):
77-
return call
78-
return lambda *args, **kwargs: asyncio.to_thread(lambda: call(*args, **kwargs))
75+
return inspect.iscoroutinefunction(fn)
76+
77+
78+
def to_async_call(fn: Callable[..., Any]) -> Callable[..., Awaitable[Any]]:
79+
if is_coroutine_fn(fn):
80+
return fn
81+
return lambda *args, **kwargs: asyncio.to_thread(fn, *args, **kwargs)

0 commit comments

Comments
 (0)