Skip to content
Open
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
43 changes: 32 additions & 11 deletions src/apify/_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@

def _warn_child_runs_experimental() -> None:
warnings.warn(
'Child run features (`run_name`, `abort_with_parent`, `Actor.child_runs` and `child_run_tracker`) are '
'experimental. Their behavior and interface may change in future versions.',
'Child run features (`run_name`, `abort_with_parent`, `Actor.child_runs`, `child_run_tracker` and '
'`max_concurrent_child_runs`) are experimental. Their behavior and interface may change in future versions.',
UserWarning,
# This helper, the SDK method, and its `ensure_context` wrapper sit between the warning and the caller.
stacklevel=4,
Expand Down Expand Up @@ -161,6 +161,7 @@ def __init__(
cleanup_timeout: timedelta = timedelta(seconds=30),
child_run_tracker: ChildRunTracker | Literal['default'] | None = 'default',
disable_excessive_child_run_warning: bool = False,
max_concurrent_child_runs: int | None = None,
) -> None:
"""Initialize a new instance.

Expand All @@ -180,6 +181,13 @@ def __init__(
behavior and interface may change in future versions.
disable_excessive_child_run_warning: Whether to mute the warning logged once the child run registry holds
many records.
max_concurrent_child_runs: How many child runs may be active at once, or `None` for no limit. At the
limit, `Actor.start`, `Actor.call`, `Actor.start_task` and `Actor.call_task` wait to start a new run,
in the order in which they were called, until another one finishes. A call that returns a recorded
run under its run name does not wait. It counts the recorded runs of earlier processes of this Actor
run too, but it holds only in the process that sets it, so set it in every process. Once this Actor run
is being aborted, a call that would have to wait raises a `RuntimeError`. It needs child run tracking.
The limit is experimental, so its behavior and interface may change in future versions.
"""
self._configuration = configuration
self._configure_logging = configure_logging
Expand All @@ -203,8 +211,13 @@ def __init__(
child_run_tracker = KeyValueStoreChildRunTracker(self.open_key_value_store)
elif child_run_tracker is not None:
_warn_child_runs_experimental()
if max_concurrent_child_runs is not None:
_warn_child_runs_experimental()
self._child_run_registry = ChildRunRegistry(
child_run_tracker, warn_on_excessive_runs=not disable_excessive_child_run_warning
child_run_tracker,
warn_on_excessive_runs=not disable_excessive_child_run_warning,
max_concurrent_runs=max_concurrent_child_runs,
default_client=lambda: self.apify_client,
)

self._runs_with_redirected_log: set[str | None] = set()
Expand Down Expand Up @@ -441,6 +454,7 @@ def __call__(
cleanup_timeout: timedelta = timedelta(seconds=30),
child_run_tracker: ChildRunTracker | Literal['default'] | None = 'default',
disable_excessive_child_run_warning: bool = False,
max_concurrent_child_runs: int | None = None,
) -> Self:
"""Make a new Actor instance with a non-default configuration.

Expand All @@ -456,6 +470,7 @@ def __call__(
cleanup_timeout=cleanup_timeout,
child_run_tracker=child_run_tracker,
disable_excessive_child_run_warning=disable_excessive_child_run_warning,
max_concurrent_child_runs=max_concurrent_child_runs,
)

@property
Expand Down Expand Up @@ -1232,6 +1247,7 @@ async def start(
run, _, _ = await self._start_child_run(
run_name=run_name,
client=client,
timeout=timeout,
start_run=partial(
actor_client.start,
run_input=run_input,
Expand All @@ -1241,7 +1257,6 @@ async def start(
max_total_charge_usd=max_total_charge_usd,
restart_on_error=restart_on_error,
memory_mbytes=memory_mbytes,
run_timeout=self._resolve_run_timeout(timeout),
force_permission_level=force_permission_level,
webhooks=to_client_representations(webhooks),
),
Expand Down Expand Up @@ -1436,6 +1451,7 @@ async def call(
started_run, key, is_new = await self._start_child_run(
run_name=run_name,
client=client,
timeout=timeout,
start_run=partial(
actor_client.start,
run_input=run_input,
Expand All @@ -1445,7 +1461,6 @@ async def call(
max_total_charge_usd=max_total_charge_usd,
restart_on_error=restart_on_error,
memory_mbytes=memory_mbytes,
run_timeout=self._resolve_run_timeout(timeout),
force_permission_level=force_permission_level,
webhooks=to_client_representations(webhooks),
),
Expand Down Expand Up @@ -1492,7 +1507,8 @@ async def _start_child_run(
*,
run_name: str | None,
client: ApifyClientAsync,
start_run: Callable[[], Awaitable[Run]],
start_run: Callable[..., Awaitable[Run]],
timeout: timedelta | Literal['inherit'] | None,
actor_id: str | None = None,
task_id: str | None = None,
run_input: Any = None,
Expand All @@ -1509,13 +1525,18 @@ async def _start_child_run(
The run, its key in the child run registry (`None` with child run tracking off), and whether it was newly
started.
"""

def start_run_now(**kwargs: Any) -> Awaitable[Run]:
# `inherit` gets the time left when the request goes out, after any wait for the concurrency limit.
return start_run(run_timeout=self._resolve_run_timeout(timeout), **kwargs)

if not self._child_run_registry.is_enabled:
return await start_run(), None, True
return await start_run_now(), None, True

is_implicit = run_name is None and self.configuration.auto_memoize_child_runs
if run_name is None and not is_implicit:
run, key = await self._child_run_registry.start(
client=client, start_run=start_run, abort_with_parent=bool(abort_with_parent)
client=client, start_run=start_run_now, abort_with_parent=bool(abort_with_parent)
)
return run, key, True

Expand All @@ -1533,7 +1554,7 @@ async def _start_child_run(
key,
checksum=checksum,
client=client,
start_run=start_run,
start_run=start_run_now,
abort_with_parent=abort_with_parent,
)
return run, key, is_new
Expand Down Expand Up @@ -1675,6 +1696,7 @@ async def start_task(
run, _, _ = await self._start_child_run(
run_name=run_name,
client=client,
timeout=timeout,
start_run=partial(
task_client.start,
task_input=task_input,
Expand All @@ -1683,7 +1705,6 @@ async def start_task(
max_total_charge_usd=max_total_charge_usd,
restart_on_error=restart_on_error,
memory_mbytes=memory_mbytes,
run_timeout=self._resolve_run_timeout(timeout),
webhooks=to_client_representations(webhooks),
),
task_id=task_id,
Expand Down Expand Up @@ -1780,6 +1801,7 @@ async def call_task(
started_run, key, is_new = await self._start_child_run(
run_name=run_name,
client=client,
timeout=timeout,
start_run=partial(
task_client.start,
task_input=task_input,
Expand All @@ -1788,7 +1810,6 @@ async def call_task(
max_total_charge_usd=max_total_charge_usd,
restart_on_error=restart_on_error,
memory_mbytes=memory_mbytes,
run_timeout=self._resolve_run_timeout(timeout),
webhooks=to_client_representations(webhooks),
# A request that times out is retried, and a retried start request can start a second run.
timeout='no_timeout',
Expand Down
Loading