@retry(tries=3, delay=1) def risky_op(): pass
Decorators & Metaclasses

Meaning

Automatically retries a function call a specified number of times with a fixed delay between attempts when it raises an exception. Addresses transient failures in network or I/O operations where a single attempt is insufficient. Reached for whenever calling external services or resources that may intermittently fail.

Primary Function

Error handling

Communicative Purpose

Ensures transient failures in external calls do not immediately propagate, giving the operation multiple chances to succeed.

Pattern

@retry(tries=max_attempts, delay=wait_seconds) def function_name(): body

Core Structure

@retry(tries=..., delay=...) def ...(): ...

Função primária

Error handling

Propósito comunicativo

Ensures transient failures in external calls do not immediately propagate, giving the operation multiple chances to succeed.

Situações de gatilho

Network programming: HTTP requests to unreliable APIs that may timeout or return 503. IoT systems: reading from sensors that occasionally fail to respond. File processing: uploading files to remote storage with intermittent connectivity.

Contextos

Web scraping, API clients, microservices, IoT data collection, cloud infrastructure automation

Padrão

@retry(tries=max_attempts, delay=wait_seconds) def function_name(): body

Estrutura central

@retry(tries=..., delay=...) def ...(): ...

Slots de substituição

tries: int (number of attempts), delay: float or int (seconds between attempts), func_name: identifier (function name), body: indented block of code to retry.

Colocados típicos

  • Functions performing I/O
  • network requests
  • database operations
  • often used with exponential backoff
  • jitter
  • or circuit breaker patterns.

Substituições comuns

  • Using a loop with try/except and time.sleep
  • using the tenacity library's @retry decorator
  • custom retry implementation with while loop.

Erros comuns

Setting tries too low causing insufficient retries; using zero delay leading to busy loops; forgetting to catch specific exceptions; applying retry to non-idempotent operations causing side effects.

Similar / contraste

@circuit_breaker (prevents repeated calls after failures) vs @retry (attempts again); @timeout (limits duration) vs retry (extends attempts).

Interferências

Coming from languages like Java where retry logic is often implemented via loops; may expect built-in retry annotation; in Python, need to import or define decorator.

Família do chunk

  • retry decorator

Nuance

Only appropriate for idempotent operations; excessive retries can worsen load; consider exponential backoff; ensure delay units match expectations.

Efeito pragmático

Reduces boilerplate error-handling code and improves resilience of external calls.

Dica de memória

Think of a retry button that automatically presses again after a short wait.

Nota

Best suited for idempotent operations such as idempotent I/O operations; excessive retries can worsen load; consider exponential backoff and jitter.

Upgrade path

Consider migrating to the tenacity library for advanced retry policies (exponential backoff, jitter, circuit breaker).

Frequência: MediumFormulaicidade: Semi-fixedTipo de construção: decorator patternPrioridade de aquisição: Active recallPrioridade de output: BothTag de espaçamento: Short-term

Log in to save chunks.