Skip to main content

Autoscaler

Background thread to autoscale pool workers.

Attributes

AttributeTypeDescription
poolProcessPoolThe process pool instance that this thread manages and scales based on workload.
mutexthreading.Lock = threading.Lock()A threading lock used to ensure thread-safe operations when scaling or updating concurrency limits.
max_concurrencyintThe upper limit on the number of concurrent worker processes allowed in the pool.
min_concurrencyint = 0The lower limit on the number of concurrent worker processes to maintain in the pool.
keepalivefloat = AUTOSCALE_KEEPALIVEThe minimum time interval in seconds required between a scale-up event and a subsequent scale-down event.
_last_scale_upfloat = nullA monotonic timestamp recording the last time the pool size was increased to prevent premature scaling down.
worker[Worker](../../events/state/worker.md?sid=celery_events_state_worker) = nullThe worker instance associated with this autoscaler, used to update consumer prefetch counts during scaling.

Constructor

Signature

def Autoscaler(
pool: object,
max_concurrency: int,
min_concurrency: int = 0,
worker: object = None,
keepalive: float = AUTOSCALE_KEEPALIVE,
mutex: threading.Lock = None
)

Parameters

NameTypeDescription
poolobjectThe process pool to be managed by the autoscaler.
max_concurrencyintThe maximum number of worker processes allowed in the pool.
min_concurrencyint = 0The minimum number of worker processes to maintain.
workerobject = NoneThe worker instance associated with this autoscaler.
keepalivefloat = AUTOSCALE_KEEPALIVEThe time interval to wait before scaling down processes.
mutexthreading.Lock = NoneAn optional mutex for thread-safe operations; defaults to a new threading.Lock.

Methods


body()

@classmethod
def body() - > null

Executes the main loop of the background thread to periodically evaluate and perform scaling operations.

Returns

TypeDescription
nullNothing is returned as this method runs in a continuous loop.

maybe_scale()

@classmethod
def maybe_scale(
req: object = null
) - > null

Triggers a scaling check and ensures the worker pool maintains its required state if scaling occurs.

Parameters

NameTypeDescription
reqobject = nullThe request object currently being processed, used to trigger scaling logic.

Returns

TypeDescription
nullNothing is returned.

update()

@classmethod
def update(
max: integer = null,
min: integer = null
) - > tuple

Updates the concurrency boundaries and immediately adjusts the pool size if the current process count falls outside new limits.

Parameters

NameTypeDescription
maxinteger = nullThe new maximum number of concurrent worker processes allowed.
mininteger = nullThe new minimum number of concurrent worker processes to maintain.

Returns

TypeDescription
tupleA tuple containing the updated (max_concurrency, min_concurrency) values.

scale_up()

@classmethod
def scale_up(
n: integer
) - > null

Increases the number of worker processes and updates the timestamp of the last scale-up event.

Parameters

NameTypeDescription
nintegerThe number of additional worker processes to spawn.

Returns

TypeDescription
nullNothing is returned.

scale_down()

@classmethod
def scale_down(
n: integer
) - > null

Decreases the number of worker processes, provided that the keepalive interval since the last scale-up has elapsed.

Parameters

NameTypeDescription
nintegerThe number of worker processes to terminate.

Returns

TypeDescription
nullNothing is returned.

info()

@classmethod
def info() - > dict

Returns a dictionary containing the current configuration and operational status of the autoscaler.

Returns

TypeDescription
dictA dictionary with keys 'max', 'min', 'current', and 'qty' representing the autoscaler's state.

qty()

@classmethod
def qty() - > integer

Calculates the number of reserved requests currently waiting to be processed.

Returns

TypeDescription
integerThe count of requests currently in the reserved state.

processes()

@classmethod
def processes() - > integer

Retrieves the current number of active worker processes in the pool.

Returns

TypeDescription
integerThe total count of processes currently managed by the pool.