Waiter Library for Polling and Retries

1.5.1 · active · verified Thu Apr 16

Waiter is a lightweight Python library providing utilities for delayed iteration, polling, and retrying operations. It's particularly useful for waiting for external resources, services, or conditions to become ready. The current version is 1.5.1, and it maintains an active release cadence with regular updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates using `waiter.until_successful` to poll a function that initially fails but eventually succeeds. It illustrates how to define a retriable operation and handle potential `TimeoutError`.

import time
from waiter import until_successful

# Define a function that simulates checking a service or condition
class ServiceChecker:
    def __init__(self):
        self.attempts = 0

    def check_status(self):
        self.attempts += 1
        if self.attempts < 3:
            # Simulate a temporary failure by raising an exception
            print(f"Attempt {self.attempts}: Service not ready yet...")
            raise ConnectionError("Service not available")
        else:
            # Simulate success after a few attempts
            print(f"Attempt {self.attempts}: Service is ready!")
            return f"Service ready after {self.attempts} attempts"

checker = ServiceChecker()
result = None
try:
    # Use until_successful to retry calling the check_status method.
    # It will keep retrying until the callable succeeds (doesn't raise an exception)
    # or the timeout is reached.
    result = until_successful(
        checker.check_status,
        timeout=5,  # Max time to wait in seconds
        pause=0.5   # Time to wait between retries in seconds
    )
    print(f"Polling successful: {result}")
except TimeoutError as e:
    print(f"Polling failed: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →