Autoscaler
Background thread to autoscale pool workers.
Attributes
| Attribute | Type | Description |
|---|---|---|
| pool | ProcessPool | The process pool instance that this thread manages and scales based on workload. |
| mutex | threading.Lock = threading.Lock() | A threading lock used to ensure thread-safe operations when scaling or updating concurrency limits. |
| max_concurrency | int | The upper limit on the number of concurrent worker processes allowed in the pool. |
| min_concurrency | int = 0 | The lower limit on the number of concurrent worker processes to maintain in the pool. |
| keepalive | float = AUTOSCALE_KEEPALIVE | The minimum time interval in seconds required between a scale-up event and a subsequent scale-down event. |
| _last_scale_up | float = null | A 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) = null | The 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
| Name | Type | Description |
|---|---|---|
| pool | object | The process pool to be managed by the autoscaler. |
| max_concurrency | int | The maximum number of worker processes allowed in the pool. |
| min_concurrency | int = 0 | The minimum number of worker processes to maintain. |
| worker | object = None | The worker instance associated with this autoscaler. |
| keepalive | float = AUTOSCALE_KEEPALIVE | The time interval to wait before scaling down processes. |
| mutex | threading.Lock = None | An 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
| Type | Description |
|---|---|
null | Nothing 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
| Name | Type | Description |
|---|---|---|
| req | object = null | The request object currently being processed, used to trigger scaling logic. |
Returns
| Type | Description |
|---|---|
null | Nothing 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
| Name | Type | Description |
|---|---|---|
| max | integer = null | The new maximum number of concurrent worker processes allowed. |
| min | integer = null | The new minimum number of concurrent worker processes to maintain. |
Returns
| Type | Description |
|---|---|
tuple | A 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
| Name | Type | Description |
|---|---|---|
| n | integer | The number of additional worker processes to spawn. |
Returns
| Type | Description |
|---|---|
null | Nothing 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
| Name | Type | Description |
|---|---|---|
| n | integer | The number of worker processes to terminate. |
Returns
| Type | Description |
|---|---|
null | Nothing is returned. |
info()
@classmethod
def info() - > dict
Returns a dictionary containing the current configuration and operational status of the autoscaler.
Returns
| Type | Description |
|---|---|
dict | A 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
| Type | Description |
|---|---|
integer | The 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
| Type | Description |
|---|---|
integer | The total count of processes currently managed by the pool. |