@retry(max_attempts=3) @backoff(factor=2) def unstable_call():
Decorators & Metaclasses

Meaning

This chunk applies a retry decorator with exponential backoff to a function call. It addresses transient failures in unreliable external services or networks. It is triggered when a function may fail intermittently and needs automatic retry attempts.

Primary Function

Retry mechanism

Communicative Purpose

Ensures transient failures are retried with exponential backoff to improve reliability.

Pattern

@retry(max_attempts=attempts) @backoff(factor=backoff_factor) def func():

Core Structure

@retry() @backoff() def func():

Função primária

Retry mechanism

Propósito comunicativo

Ensures transient failures are retried with exponential backoff to improve reliability.

Situações de gatilho

Web services: calling external APIs that may temporarily fail due to network glitches Cloud functions: invoking unreliable third-party services with sporadic downtime Data pipelines: reading from flaky network storage that occasionally times out

Contextos

Python web applications, microservices, distributed systems

Padrão

@retry(max_attempts=attempts) @backoff(factor=backoff_factor) def func():

Estrutura central

@retry() @backoff() def func():

Slots de substituição

attempts: int >= 1, backoff_factor: float > 0, func: function name (identifier)

Colocados típicos

  • idempotent operations
  • circuit breaker
  • timeout settings
  • exception handling

Substituições comuns

  • manual retry loop: more control but verbose
  • tenacity library: more features but extra dependency
  • exponential backoff with jitter: reduces thundering herd

Erros comuns

forgetting to await async function: leads to unawaited coroutine and silent failure setting max_attempts too low: gives up too soon, reducing reliability using backoff factor <= 0: causes invalid or negative delay, breaking backoff logic catching BaseException: hides keyboard interrupt and system exits, making termination difficult not preserving function signature: loses metadata like __name__ and docstring, affecting debugging

Similar / contraste

circuit breaker: stops retrying after a failure threshold to prevent overload timeout: limits duration per attempt rather than retrying after failure rate limiting: controls request frequency instead of retrying failed requests

Interferências

Coming from Java: may use try-catch loop instead of decorator — Python's decorators provide cleaner separation of concerns Coming from Go: may forget to handle returned error — decorator automates retry and error handling transparently

Família do chunk

  • retry pattern
  • exponential backoff
  • circuit breaker
  • timeout decorator

Nuance

Do not use for non-idempotent operations where retries could cause unintended side effects Adds latency proportional to attempt count and backoff factor; consider jitter to avoid thundering herd Ensure the decorated function does not rely on mutable global state that could change between retries

Efeito pragmático

Reduces failure rates in distributed systems by automatically recovering from transient errors without manual intervention.

Dica de memória

Retry decorator: like a safety net that catches you when you slip and lifts you back up to try again.

Upgrade path

@retry(stop_max_attempt_number=3, wait_exponential_multiplier=1000) def resilient_call():

Frequência: HighFormulaicidade: Semi-fixedPrioridade de aquisição: Active recallPrioridade de output: BothTag de espaçamento: Medium-term

Log in to save chunks.