[![image](https://img.shields.io/pypi/v/waiter.svg)](https://pypi.org/project/waiter/)
![image](https://img.shields.io/pypi/pyversions/waiter.svg)
[![image](https://pepy.tech/badge/waiter)](https://pepy.tech/project/waiter)
![image](https://img.shields.io/pypi/status/waiter.svg)
[![image](https://github.com/coady/waiter/workflows/build/badge.svg)](https://github.com/coady/waiter/actions)
[![image](https://codecov.io/gh/coady/waiter/branch/main/graph/badge.svg)](https://codecov.io/gh/coady/waiter/)
[![image](https://github.com/coady/waiter/workflows/codeql/badge.svg)](https://github.com/coady/waiter/security/code-scanning)
[![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)
[![image](https://mypy-lang.org/static/mypy_badge.svg)](https://mypy-lang.org/)
Does 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.
Waiter 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.
## Usage
### creation
Supply a number of seconds to repeat endlessly, or any iterable of seconds.
```python
from waiter import wait
wait(1) # 1, 1, 1, 1, ...
wait([1] * 3) # 1, 1, 1
wait([0.5, 0.5, 60]) # circuit breaker
```
Iterable delays can express any waiting strategy, and constructors for common algorithms are also provided.
```python
wait.count(1) # incremental backoff 1, 2, 3, 4, 5, ...
wait(1) + 1 # alternate syntax 1, 2, 3, 4, 5, ...
wait.fibonacci(1) # 1, 1, 2, 3, 5, ...
wait.polynomial(2) # 0, 1, 4, 9, 16, ...
wait.exponential(2) # exponential backoff 1, 2, 4, 8, ...
backoff = wait(1) * 2 # alternate syntax 1, 2, 4, 8, ...
backoff[:3] # limit attempt count 1, 2, 4
backoff <= 5 # set maximum delay 1, 2, 4, 5, 5, 5, ...
backoff.random(-1, 1) # add random jitter
```
### iteration
Then simply use the `wait` object like any iterable, yielding the amount of elapsed time. Timeouts also supported of course.
```python
from waiter import wait, suppress, first
for elapsed in wait(delays): # first iteration is immediate
with suppress(exception): # then each subsequent iteration sleeps as necessary
...
break
for _ in wait(delays, timeout): # standard convention for ignoring a loop variable
... # won't sleep past the timeout
if ...:
break
results = (... for _ in wait(delays)) # expressions are even easier
first(predicate, results[, default]) # filter for first true item
assert any(results) # perfect for tests too
```
### functions
Yes, functional versions are provided, as well as being trivial to implement.
```python
wait(...).throttle(iterable) # generate items from iterable
wait(...).repeat(func, *args, **kwargs) # generate successive results
wait(...).retry(exception, func, *args, **kwargs) # return first success or re-raise exception
wait(...).poll(predicate, func, *args, **kwargs) # return first success or raise StopIteration
```
The decorator variants are simply partial applications of the corresponding methods. Note decorator syntax doesn't support arbitrary expressions.
```python
backoff = wait(0.1) * 2
@backoff.repeating
@backoff.retrying(exception)
@backoff.polling(predicate)
```
But in the real world:
* the function may not exist or be succinctly written as a lambda
* the predicate may not exist or be succinctly written as a lambda
* logging may be required
* there may be complex handling of different exceptions or results
So 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.
### async
Waiters also support async iteration. `throttle` optionally accepts an async iterable. `repeat`, `retry`, and `poll` optionally accept coroutine functions.
### statistics
Waiter objects have a `stats` attribute for aggregating statistics about the calls made. The base implementation provides `total` and `failure` counts. The interface of the `stats` object itself is considered provisional for now, but can be extended by overriding the `Stats` class attribute. This also allows customization of the iterable values; elapsed time is the default.
## Installation
```console
% pip install waiter
```
## Dependencies
* multimethod
## Tests
100% branch coverage.
```console
% pytest [--cov]
```
Raw data
{
"_id": null,
"home_page": "",
"name": "waiter",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": "",
"keywords": "wait,retry,poll,delay,sleep,timeout,incremental,exponential,backoff,async",
"author": "",
"author_email": "Aric Coady <aric.coady@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/86/95/773cdabe7dbe0b020cd1867a567d89179d0187db90a3978fa9d688f58710/waiter-1.4.tar.gz",
"platform": null,
"description": "[![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[![image](https://github.com/coady/waiter/workflows/build/badge.svg)](https://github.com/coady/waiter/actions)\n[![image](https://codecov.io/gh/coady/waiter/branch/main/graph/badge.svg)](https://codecov.io/gh/coady/waiter/)\n[![image](https://github.com/coady/waiter/workflows/codeql/badge.svg)](https://github.com/coady/waiter/security/code-scanning)\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 <= 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 simply partial applications of the corresponding methods. Note decorator syntax doesn't support arbitrary expressions.\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 provides `total` and `failure` counts. The interface of the `stats` object itself is considered provisional for now, but can be extended by overriding the `Stats` class attribute. This also allows customization of the iterable values; elapsed time is the default.\n\n## Installation\n```console\n% pip install waiter\n```\n\n## Dependencies\n* multimethod\n\n## Tests\n100% branch coverage.\n\n```console\n% pytest [--cov]\n```\n",
"bugtrack_url": null,
"license": "Copyright 2022 Aric Coady Licensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ",
"summary": "Delayed iteration for polling and retries.",
"version": "1.4",
"project_urls": {
"Changelog": "https://github.com/coady/waiter/blob/main/CHANGELOG.md",
"Documentation": "https://coady.github.io/waiter",
"Homepage": "https://github.com/coady/waiter",
"Issues": "https://github.com/coady/waiter/issues"
},
"split_keywords": [
"wait",
"retry",
"poll",
"delay",
"sleep",
"timeout",
"incremental",
"exponential",
"backoff",
"async"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "6760d2db247e8ff3f4cb332ee772133260456d1b7bd7a374ab46434f630d479a",
"md5": "3f7e37fd3a8910ea081d40bbf768d52d",
"sha256": "78ad350e4e4fa943db129de33f5422f76bfcadec3e6f0433adecc45ddd945d8a"
},
"downloads": -1,
"filename": "waiter-1.4-py3-none-any.whl",
"has_sig": false,
"md5_digest": "3f7e37fd3a8910ea081d40bbf768d52d",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 6870,
"upload_time": "2023-11-10T01:25:23",
"upload_time_iso_8601": "2023-11-10T01:25:23.820863Z",
"url": "https://files.pythonhosted.org/packages/67/60/d2db247e8ff3f4cb332ee772133260456d1b7bd7a374ab46434f630d479a/waiter-1.4-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "8695773cdabe7dbe0b020cd1867a567d89179d0187db90a3978fa9d688f58710",
"md5": "3e3b5ce9c53c19d35f0da6be8d48c53a",
"sha256": "f4827acf132b42565ba1f71a9a882d1f28446e8cfef50962cbfefd65cbbd2902"
},
"downloads": -1,
"filename": "waiter-1.4.tar.gz",
"has_sig": false,
"md5_digest": "3e3b5ce9c53c19d35f0da6be8d48c53a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 10017,
"upload_time": "2023-11-10T01:25:31",
"upload_time_iso_8601": "2023-11-10T01:25:31.393980Z",
"url": "https://files.pythonhosted.org/packages/86/95/773cdabe7dbe0b020cd1867a567d89179d0187db90a3978fa9d688f58710/waiter-1.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-11-10 01:25:31",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "coady",
"github_project": "waiter",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "waiter"
}