Skip to content

Add optional args= param to ctx.run #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions python/restate/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
I = TypeVar('I')
O = TypeVar('O')

RunAction = Union[Callable[[], T], Callable[[], Awaitable[T]]]
RunAction = Union[Callable[..., T], Callable[..., Awaitable[T]]]


# pylint: disable=R0903
Expand Down Expand Up @@ -154,7 +154,8 @@ def run(self,
serde: Serde[T] = DefaultSerde(),
max_attempts: typing.Optional[int] = None,
max_retry_duration: typing.Optional[timedelta] = None,
type_hint: Optional[typing.Type[T]] = None
type_hint: Optional[typing.Type[T]] = None,
args: Optional[typing.Tuple[Any, ...]] = None,
) -> RestateDurableFuture[T]:
"""
Runs the given action with the given name.
Expand Down
19 changes: 15 additions & 4 deletions python/restate/server_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import asyncio
from datetime import timedelta
import inspect
import functools
from typing import Any, Awaitable, Callable, Dict, List, Optional, TypeVar
import typing
import traceback
Expand Down Expand Up @@ -397,7 +398,8 @@ async def create_run_coroutine(self,
action: Callable[[], T] | Callable[[], Awaitable[T]],
serde: Serde[T],
max_attempts: Optional[int] = None,
max_retry_duration: Optional[timedelta] = None):
max_retry_duration: Optional[timedelta] = None,
):
"""Create a coroutine to poll the handle."""
try:
if inspect.iscoroutinefunction(action):
Expand All @@ -423,20 +425,29 @@ async def create_run_coroutine(self,
# pylint: disable=R0914
def run(self,
name: str,
action: Callable[[], T] | Callable[[], Awaitable[T]],
action: Callable[..., T] | Callable[..., Awaitable[T]],
serde: Serde[T] = DefaultSerde(),
max_attempts: Optional[int] = None,
max_retry_duration: Optional[timedelta] = None,
type_hint: Optional[typing.Type[T]] = None
type_hint: Optional[typing.Type[T]] = None,
args: Optional[typing.Tuple[Any, ...]] = None
) -> RestateDurableFuture[T]:

if isinstance(serde, DefaultSerde):
if type_hint is None:
signature = inspect.signature(action, eval_str=True)
type_hint = signature.return_annotation
serde = serde.with_maybe_type(type_hint)

handle = self.vm.sys_run(name)
self.run_coros_to_execute[handle] = lambda : self.create_run_coroutine(handle, action, serde, max_attempts, max_retry_duration)

if args is not None:
noargs_action = functools.partial(action, *args)
else:
# todo: we can also verify by looking at the signature that there are no missing parameters
noargs_action = action # type: ignore

self.run_coros_to_execute[handle] = lambda : self.create_run_coroutine(handle, noargs_action, serde, max_attempts, max_retry_duration)
return self.create_future(handle, serde) # type: ignore


Expand Down