Repository: apify/crawlee-python
File: src/crawlee/_autoscaling/autoscaled_pool.py (Python)

Code before:

    _LOGGING_INTERVAL = timedelta(minutes=1)
    """Interval at which the autoscaled pool logs its current state."""

    _DESIRED_CONCURRENCY_RATIO = 0.9
    """Minimum ratio of desired concurrency that must be reached before allowing further scale-up."""

    _SCALE_UP_STEP_RATIO = 0.05
    """Fraction of desired concurrency to add during each scale-up operation."""

    _SCALE_DOWN_STEP_RATIO = 0.05
    """Fraction of desired concurrency to remove during each scale-down operation."""

    _TASK_TIMEOUT: timedelta | None = None
    """Timeout within which the `run_task_function` must complete."""

    def __init__(
        self,
        *,
        system_status: SystemStatus,
        concurrency_settings: ConcurrencySettings | None = None,
        run_task_function: Callable[[], Awaitable],
        is_task_ready_function: Callable[[], Awaitable[bool]],
        is_finished_function: Callable[[], Awaitable[bool]],
    ) -> None:
        """Create a new instance.

        Args:
            system_status: Provides data about system utilization (load).
            concurrency_settings: Settings of concurrency levels.
            run_task_function: A function that performs an asynchronous resource-intensive task.
            is_task_ready_function: A function that indicates whether `run_task_function` should be called. This
                function is called every time there is free capacity for a new task and it should indicate whether
                it should start a new task or not by resolving to either `True` or `False`. Besides its obvious use,
                it is also useful for task throttling to save resources.
            is_finished_function: A function that is called only when there are no tasks to be processed. If it
                resolves to `True` then the pool's run finishes. Being called only when there are no tasks being
                processed means that as long as `is_task_ready_function` keeps resolving to `True`,
                `is_finished_function` will never be called. To abort a run, use the `abort` method.
        """
        concurrency_settings = concurrency_settings or ConcurrencySettings()

        self._system_status = system_status
        self._run_task_function = run_task_function
        self._is_task_ready_function = is_task_ready_function
        self._is_finished_function = is_finished_function
        self._desired_concurrency = concurrency_settings.desired_concurrency
        self._max_concurrency = concurrency_settings.max_concurrency
        self._min_concurrency = concurrency_settings.min_concurrency
        self._max_tasks_per_minute = concurrency_settings.max_tasks_per_minute


Review comment (suggestion):
`__init__` is not a constructor, it does not create the instance, it initializes it :slightly_smiling_face: Also, I'd prefer to just leave this blank, but I guess it's not possible to just have the `Args` section in a docblock.

Code after:

    _LOGGING_INTERVAL = timedelta(minutes=1)
    """Interval at which the autoscaled pool logs its current state."""

    _DESIRED_CONCURRENCY_RATIO = 0.9
    """Minimum ratio of desired concurrency that must be reached before allowing further scale-up."""

    _SCALE_UP_STEP_RATIO = 0.05
    """Fraction of desired concurrency to add during each scale-up operation."""

    _SCALE_DOWN_STEP_RATIO = 0.05
    """Fraction of desired concurrency to remove during each scale-down operation."""

    _TASK_TIMEOUT: timedelta | None = None
    """Timeout within which the `run_task_function` must complete."""

    def __init__(
        self,
        *,
        system_status: SystemStatus,
        concurrency_settings: ConcurrencySettings | None = None,
        run_task_function: Callable[[], Awaitable],
        is_task_ready_function: Callable[[], Awaitable[bool]],
        is_finished_function: Callable[[], Awaitable[bool]],
    ) -> None:
        """Initialize a new instance.

        Args:
            system_status: Provides data about system utilization (load).
            concurrency_settings: Settings of concurrency levels.
            run_task_function: A function that performs an asynchronous resource-intensive task.
            is_task_ready_function: A function that indicates whether `run_task_function` should be called. This
                function is called every time there is free capacity for a new task and it should indicate whether
                it should start a new task or not by resolving to either `True` or `False`. Besides its obvious use,
                it is also useful for task throttling to save resources.
            is_finished_function: A function that is called only when there are no tasks to be processed. If it
                resolves to `True` then the pool's run finishes. Being called only when there are no tasks being
                processed means that as long as `is_task_ready_function` keeps resolving to `True`,
                `is_finished_function` will never be called. To abort a run, use the `abort` method.
        """
        concurrency_settings = concurrency_settings or ConcurrencySettings()

        self._system_status = system_status
        self._run_task_function = run_task_function
        self._is_task_ready_function = is_task_ready_function
        self._is_finished_function = is_finished_function
        self._desired_concurrency = concurrency_settings.desired_concurrency
        self._max_concurrency = concurrency_settings.max_concurrency
        self._min_concurrency = concurrency_settings.min_concurrency
        self._max_tasks_per_minute = concurrency_settings.max_tasks_per_minute