From 9205d90f28887a5d718c41b029b6f6868382b5c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Ram=C3=ADrez=20Mondrag=C3=B3n?= Date: Sun, 30 Aug 2026 11:58:59 -0600 Subject: [PATCH] docs: Added an example to the docstring of `backoff.runtime()` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Edgar Ramírez Mondragón --- backoff/_wait_gen.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/backoff/_wait_gen.py b/backoff/_wait_gen.py index 7a43829..5f0e251 100644 --- a/backoff/_wait_gen.py +++ b/backoff/_wait_gen.py @@ -103,6 +103,27 @@ def runtime(*, value: Callable[[Any], float]) -> Generator[float, Any, None]: """Generator that is based on parsing the return value or thrown exception of the decorated method + Useful for honoring a server-specified retry delay, e.g. an HTTP + `Retry-After` header, rather than a fixed wait sequence: + + # with on_predicate, `value` receives the return value + @backoff.on_predicate( + backoff.runtime, + predicate=lambda r: r.status_code == 429, + value=lambda r: int(r.headers.get("Retry-After", 1)), + ) + def get_page(): + return requests.get(url) + + # with on_exception, `value` receives the raised exception + @backoff.on_exception( + backoff.runtime, + RetryableError, + value=lambda e: e.wait_seconds, + ) + def get_page(): + ... + Args: value: a callable which takes as input the decorated function's return value or thrown exception and