From 1950ad8806be665282a3d4cfe026a541dcfc3937 Mon Sep 17 00:00:00 2001 From: Achille Roussel Date: Tue, 20 Feb 2024 11:00:29 -0800 Subject: [PATCH 1/2] fix coroutine decoration Signed-off-by: Achille Roussel --- src/dispatch/function.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/dispatch/function.py b/src/dispatch/function.py index 798af504..e9b26459 100644 --- a/src/dispatch/function.py +++ b/src/dispatch/function.py @@ -59,6 +59,7 @@ def __init__( name: str, primitive_func: PrimitiveFunctionType, func: Callable, + coroutine: bool = False, ): self._endpoint = endpoint self._client = client @@ -66,7 +67,7 @@ def __init__( self._primitive_func = primitive_func # FIXME: is there a way to decorate the function at the definition # without making it a class method? - if inspect.iscoroutinefunction(func): + if coroutine: self._func = durable(self._call_async) else: self._func = func @@ -204,7 +205,7 @@ def primitive_func(input: Input) -> Output: primitive_func.__qualname__ = f"{func.__qualname__}_primitive" primitive_func = durable(primitive_func) - return self._register(func, primitive_func) + return self._register(primitive_func, func, coroutine=False) def _register_coroutine(self, func: Callable) -> Function: logger.info("registering coroutine: %s", func.__qualname__) @@ -218,14 +219,14 @@ def primitive_func(input: Input) -> Output: primitive_func.__qualname__ = f"{func.__qualname__}_primitive" primitive_func = durable(primitive_func) - return self._register(func, primitive_func) + return self._register(primitive_func, func, coroutine=True) def _register_primitive_function(self, func: PrimitiveFunctionType) -> Function: logger.info("registering primitive function: %s", func.__qualname__) - return self._register(func, func) + return self._register(func, func, coroutine=inspect.iscoroutinefunction(func)) def _register( - self, func: Callable, primitive_func: PrimitiveFunctionType + self, primitive_func: PrimitiveFunctionType, func: Callable, coroutine: bool ) -> Function: name = func.__qualname__ if name in self._functions: @@ -233,7 +234,7 @@ def _register( f"function or coroutine already registered with name '{name}'" ) wrapped_func = Function( - self._endpoint, self._client, name, primitive_func, func + self._endpoint, self._client, name, primitive_func, func, coroutine ) self._functions[name] = wrapped_func return wrapped_func From fe5e04aaf5790afb5bdef23e19a246f83967d32b Mon Sep 17 00:00:00 2001 From: Achille Roussel Date: Tue, 20 Feb 2024 11:06:20 -0800 Subject: [PATCH 2/2] cleanup Signed-off-by: Achille Roussel --- src/dispatch/function.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/dispatch/function.py b/src/dispatch/function.py index e9b26459..c996ff20 100644 --- a/src/dispatch/function.py +++ b/src/dispatch/function.py @@ -67,10 +67,7 @@ def __init__( self._primitive_func = primitive_func # FIXME: is there a way to decorate the function at the definition # without making it a class method? - if coroutine: - self._func = durable(self._call_async) - else: - self._func = func + self._func = durable(self._call_async) if coroutine else func def __call__(self, *args, **kwargs): return self._func(*args, **kwargs)