{"id":13725219,"url":"https://github.com/coady/waiter","last_synced_at":"2025-04-06T21:16:47.284Z","repository":{"id":6582207,"uuid":"54683816","full_name":"coady/waiter","owner":"coady","description":"Delayed iteration for polling and retries.","archived":false,"fork":false,"pushed_at":"2024-11-18T01:02:12.000Z","size":777,"stargazers_count":30,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-30T19:09:06.257Z","etag":null,"topics":["asyncio","backoff","delay","exponential","incremental","poll","retry","sleep","timeout","wait"],"latest_commit_sha":null,"homepage":"https://coady.github.io/waiter","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/coady.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-03-25T00:47:35.000Z","updated_at":"2024-11-18T01:02:15.000Z","dependencies_parsed_at":"2023-11-07T05:04:49.087Z","dependency_job_id":"bec4e5f4-8031-41b2-9a97-199400c4d5a5","html_url":"https://github.com/coady/waiter","commit_stats":{"total_commits":76,"total_committers":2,"mean_commits":38.0,"dds":0.07894736842105265,"last_synced_commit":"7199a4d7b8d11b7ef8acbf17832bbd367fad147d"},"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coady%2Fwaiter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coady%2Fwaiter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coady%2Fwaiter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coady%2Fwaiter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/coady","download_url":"https://codeload.github.com/coady/waiter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247550693,"owners_count":20956987,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asyncio","backoff","delay","exponential","incremental","poll","retry","sleep","timeout","wait"],"created_at":"2024-08-03T01:02:16.163Z","updated_at":"2025-04-06T21:16:47.247Z","avatar_url":"https://github.com/coady.png","language":"Python","readme":"[![image](https://img.shields.io/pypi/v/waiter.svg)](https://pypi.org/project/waiter/)\n![image](https://img.shields.io/pypi/pyversions/waiter.svg)\n[![image](https://pepy.tech/badge/waiter)](https://pepy.tech/project/waiter)\n![image](https://img.shields.io/pypi/status/waiter.svg)\n[![build](https://github.com/coady/waiter/actions/workflows/build.yml/badge.svg)](https://github.com/coady/waiter/actions/workflows/build.yml)\n[![image](https://codecov.io/gh/coady/waiter/branch/main/graph/badge.svg)](https://codecov.io/gh/coady/waiter/)\n[![CodeQL](https://github.com/coady/waiter/actions/workflows/github-code-scanning/codeql/badge.svg)](https://github.com/coady/waiter/actions/workflows/github-code-scanning/codeql)\n[![image](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)\n[![image](https://mypy-lang.org/static/mypy_badge.svg)](https://mypy-lang.org/)\n\nDoes Python need yet another retry / poll library? It needs at least one that isn't coupled to decorators and functions. Decorators prevent the caller from customizing delay options, and organizing the code around functions hinders any custom handling of failures.\n\nWaiter is built around iteration instead, because the foundation of retrying / polling is a slowly executing loop. The resulting interface is both easier to use and more flexible, decoupling the delay algorithms from the application logic.\n\n## Usage\n### creation\nSupply a number of seconds to repeat endlessly, or any iterable of seconds.\n\n```python\nfrom waiter import wait\n\nwait(1)                 # 1, 1, 1, 1, ...\nwait([1] * 3)           # 1, 1, 1\nwait([0.5, 0.5, 60])    # circuit breaker\n```\n\nIterable delays can express any waiting strategy, and constructors for common algorithms are also provided.\n\n```python\nwait.count(1)           # incremental backoff 1, 2, 3, 4, 5, ...\nwait(1) + 1             # alternate syntax 1, 2, 3, 4, 5, ...\nwait.fibonacci(1)       # 1, 1, 2, 3, 5, ...\nwait.polynomial(2)      # 0, 1, 4, 9, 16, ...\n\nwait.exponential(2)     # exponential backoff 1, 2, 4, 8, ...\nbackoff = wait(1) * 2   # alternate syntax 1, 2, 4, 8, ...\nbackoff[:3]             # limit attempt count 1, 2, 4\nbackoff \u003c= 5            # set maximum delay   1, 2, 4, 5, 5, 5, ...\nbackoff.random(-1, 1)   # add random jitter\n```\n\n### iteration\nThen simply use the `wait` object like any iterable, yielding the amount of elapsed time. Timeouts also supported of course.\n\n```python\nfrom waiter import wait, suppress, first\n\nfor elapsed in wait(delays):            # first iteration is immediate\n    with suppress(exception):           # then each subsequent iteration sleeps as necessary\n        ...\n        break\n\nfor _ in wait(delays, timeout):         # standard convention for ignoring a loop variable\n    ...                                 # won't sleep past the timeout\n    if ...:\n        break\n\nresults = (... for _ in wait(delays))   # expressions are even easier\nfirst(predicate, results[, default])    # filter for first true item\nassert any(results)                     # perfect for tests too\n```\n\n### functions\nYes, functional versions are provided, as well as being trivial to implement.\n\n```python\nwait(...).throttle(iterable)                      # generate items from iterable\nwait(...).repeat(func, *args, **kwargs)           # generate successive results\nwait(...).retry(exception, func, *args, **kwargs) # return first success or re-raise exception\nwait(...).poll(predicate, func, *args, **kwargs)  # return first success or raise StopIteration\n```\n\nThe decorator variants are partial applications of the corresponding methods.\n\n```python\nbackoff = wait(0.1) * 2\n@backoff.repeating\n@backoff.retrying(exception)\n@backoff.polling(predicate)\n```\n\nBut in the real world:\n* the function may not exist or be succinctly written as a lambda\n* the predicate may not exist or be succinctly written as a lambda\n* logging may be required\n* there may be complex handling of different exceptions or results\n\nSo consider the block form, just as decorators don't render `with` blocks superfluous. Also note `wait` objects are re-iterable provided their original delays were.\n\n### async\nWaiters also support async iteration. `throttle` optionally accepts an async iterable. `repeat`, `retry`, and `poll` optionally accept coroutine functions.\n\n### statistics\nWaiter objects have a `stats` attribute for aggregating statistics about the calls made. The base implementation is an attempt counter. The interface of the `stats` object itself is considered provisional, but can be extended by overriding the `Stats` class attribute. The `add` method also allows customization of the iterable values; elapsed time is the default.\n\n## Installation\n```console\n% pip install waiter\n```\n\n## Tests\n100% branch coverage.\n\n```console\n% pytest [--cov]\n```\n","funding_links":[],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoady%2Fwaiter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcoady%2Fwaiter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoady%2Fwaiter/lists"}