RetryPolicy Class
Retry configuration for resilient tasks.
New in version 2.1.0.
Constructor
RetryPolicy(*, initial_delay: timedelta | float = datetime.timedelta(seconds=1), backoff_coefficient: float = 2.0, max_delay: timedelta | float = datetime.timedelta(seconds=60), max_attempts: int = 3, retry_on: type[Exception] | tuple[type[Exception], ...] | None = None, jitter: bool | float = True, _linear: bool = False)
Parameters
| Name | Description |
|---|---|
|
initial_delay
Required
|
Base delay between retries. |
|
backoff_coefficient
Required
|
Multiplier applied per attempt. |
|
max_delay
Required
|
Upper bound on computed delay. |
|
max_attempts
Required
|
Total attempts (including the first try). This is a
single resilient budget that counts handler-raised failures across
ALL lifetimes — the count is persisted to
|
|
retry_on
Required
|
Exception types that trigger retry. |
|
jitter
Required
|
Whether to add ±25% randomization to delays. |
Keyword-Only Parameters
| Name | Description |
|---|---|
|
initial_delay
|
Default value: 0:00:01
|
|
backoff_coefficient
|
Default value: 2.0
|
|
max_delay
|
Default value: 0:01:00
|
|
max_attempts
|
Default value: 3
|
|
retry_on
|
Default value: None
|
|
jitter
|
Default value: True
|
|
_linear
|
Default value: False
|
Methods
| compute_delay |
Return the delay in seconds for the given attempt (0-indexed). |
| exponential_backoff |
Exponential backoff — the most common pattern. Delay doubles per attempt: 1 s → 2 s → 4 s → ... capped at max_delay. |
| fixed_delay |
Fixed delay — constant interval between retries. Useful for rate-limited APIs where you want to wait a fixed amount of time between each attempt. |
| linear_backoff |
Linear backoff — delay grows additively. Delay is |
| no_retry |
No retry — the function runs once and fails on exception. Equivalent to not setting a retry policy at all. |
| should_retry |
Return whether the task should be retried. |
compute_delay
exponential_backoff
Exponential backoff — the most common pattern.
Delay doubles per attempt: 1 s → 2 s → 4 s → ... capped at max_delay.
exponential_backoff(*, max_attempts: int = 3, initial_delay: timedelta = datetime.timedelta(seconds=1), max_delay: timedelta = datetime.timedelta(seconds=60), backoff_coefficient: float = 2.0, jitter: bool = True) -> RetryPolicy
Keyword-Only Parameters
| Name | Description |
|---|---|
|
max_attempts
|
Total attempts including the first try. Default value: 3
|
|
initial_delay
|
Base delay. Default value: 0:00:01
|
|
max_delay
|
Upper bound. Default value: 0:01:00
|
|
backoff_coefficient
|
Multiplier applied per attempt. Default value: 2.0
|
|
jitter
|
Add ±25% randomization. Default value: True
|
Returns
| Type | Description |
|---|---|
|
A configured |
fixed_delay
Fixed delay — constant interval between retries.
Useful for rate-limited APIs where you want to wait a fixed amount of time between each attempt.
fixed_delay(*, delay: timedelta = datetime.timedelta(seconds=5), max_attempts: int = 3) -> RetryPolicy
Keyword-Only Parameters
| Name | Description |
|---|---|
|
delay
|
Constant delay between retries. Default value: 0:00:05
|
|
max_attempts
|
Total attempts including the first try. Default value: 3
|
Returns
| Type | Description |
|---|---|
|
A configured |
linear_backoff
Linear backoff — delay grows additively.
Delay is initial_delay * (attempt + 1): 1 s → 2 s → 3 s → ...
linear_backoff(*, initial_delay: timedelta = datetime.timedelta(seconds=1), max_delay: timedelta = datetime.timedelta(seconds=60), max_attempts: int = 5) -> RetryPolicy
Keyword-Only Parameters
| Name | Description |
|---|---|
|
initial_delay
|
Base delay unit. Default value: 0:00:01
|
|
max_delay
|
Upper bound. Default value: 0:01:00
|
|
max_attempts
|
Total attempts including the first try. Default value: 5
|
Returns
| Type | Description |
|---|---|
|
A configured |
no_retry
No retry — the function runs once and fails on exception.
Equivalent to not setting a retry policy at all.
no_retry() -> RetryPolicy
Returns
| Type | Description |
|---|---|
|
A |
should_retry
Return whether the task should be retried.
should_retry(attempt: int, error: Exception) -> bool
Parameters
| Name | Description |
|---|---|
|
attempt
Required
|
The 0-based attempt number that just failed. |
|
error
Required
|
The exception that was raised. |
Returns
| Type | Description |
|---|---|
|
|