|
1 |
| -# aiodike - Python asyncio tools for web service resilience |
| 1 | +# dike - Python asyncio tools for web service resilience |
2 | 2 |
|
3 |
| -[<img src="https://img.shields.io/pypi/v/aiodike.svg" alt="Release Status">](https://pypi.python.org/pypi/aiodike) |
4 |
| -[<img src="https://github.com/chr1st1ank/aiodike/actions/workflows/test.yml/badge.svg?branch=main" alt="CI Status">](https://github.com/chr1st1ank/aiodike/actions) |
5 |
| -[](https://codecov.io/gh/chr1st1ank/aiodike) |
| 3 | +[<img src="https://img.shields.io/pypi/v/dike.svg" alt="Release Status">](https://pypi.python.org/pypi/dike) |
| 4 | +[<img src="https://github.com/chr1st1ank/dike/actions/workflows/test.yml/badge.svg?branch=main" alt="CI Status">](https://github.com/chr1st1ank/dike/actions) |
| 5 | +[](https://codecov.io/gh/chr1st1ank/dike) |
6 | 6 |
|
7 | 7 |
|
8 |
| -* Documentation: <https://chr1st1ank.github.io/aiodike/> |
| 8 | +* Documentation: <https://chr1st1ank.github.io/dike/> |
9 | 9 | * License: Apache-2.0
|
10 | 10 | * Status: Initial development
|
11 | 11 |
|
12 | 12 | ## Features
|
13 | 13 |
|
14 | 14 | ### Concurrency limiting for asynchronous functions
|
15 | 15 | The `@limit_jobs` decorator allows to limit the number of concurrent excecutions of a coroutine
|
16 |
| -function. |
| 16 | +function. This can be useful for limiting queueing times or for limiting the load put |
| 17 | +onto backend services. |
| 18 | + |
| 19 | +Example with an external web request using the [httpx](https://github.com/encode/httpx) library: |
| 20 | + |
| 21 | +```python |
| 22 | +import asyncio |
| 23 | +import httpx |
| 24 | +import dike |
| 25 | + |
| 26 | + |
| 27 | +@dike.limit_jobs(limit=2) |
| 28 | +async def web_request(): |
| 29 | + async with httpx.AsyncClient() as client: |
| 30 | + response = await client.get("https://httpstat.us/200?sleep=100") |
| 31 | + return response |
| 32 | + |
| 33 | + |
| 34 | +async def main(): |
| 35 | + responses = await asyncio.gather( |
| 36 | + web_request(), web_request(), web_request(), return_exceptions=True |
| 37 | + ) |
| 38 | + for r in responses: |
| 39 | + if isinstance(r, dike.TooManyCalls): |
| 40 | + print("too many calls") |
| 41 | + else: |
| 42 | + print(r) |
| 43 | + |
| 44 | + |
| 45 | +asyncio.run(main()) |
| 46 | +``` |
| 47 | + |
| 48 | +The output shows that the first two requests succeed. The third one hits the concurrency limit: |
| 49 | +``` |
| 50 | +<Response [200 OK]> |
| 51 | +<Response [200 OK]> |
| 52 | +too many calls |
| 53 | +``` |
17 | 54 |
|
18 | 55 | ### Mini-batching for asynchronous function calls
|
19 | 56 | The `@batch` decorator groups function calls into batches and only calls the wrapped function
|
20 |
| -once on the collected input. |
| 57 | +with the aggregated input. |
| 58 | + |
| 59 | +This is useful if the function scales well with the size of the input arguments but you're |
| 60 | +getting the input data in smaller bits, e.g. as individual HTTP requests. |
| 61 | + |
| 62 | +Example: |
| 63 | + |
| 64 | +```python |
| 65 | +import asyncio |
| 66 | +import dike |
| 67 | + |
| 68 | + |
| 69 | +@dike.batch(target_batch_size=3, max_waiting_time=10) |
| 70 | +async def f(arg1, arg2): |
| 71 | + print(f"arg1: {arg1}") |
| 72 | + print(f"arg2: {arg2}") |
| 73 | + return [10, 11, 12] |
| 74 | + |
| 75 | + |
| 76 | +async def main(): |
| 77 | + result = await asyncio.gather( |
| 78 | + f([0], ["a"]), |
| 79 | + f([1], ["b"]), |
| 80 | + f([2], ["c"]), |
| 81 | + ) |
| 82 | + |
| 83 | + print(f"Result: {result}") |
| 84 | + |
| 85 | + |
| 86 | +asyncio.run(main()) |
| 87 | +``` |
| 88 | + |
| 89 | +Output: |
| 90 | +``` |
| 91 | +arg1: [0, 1, 2] |
| 92 | +arg2: ['a', 'b', 'c'] |
| 93 | +Result: [[10], [11], [12]] |
| 94 | +``` |
21 | 95 |
|
22 | 96 | ## Installation
|
| 97 | +Simply install from pypi. The library is pure Python without any dependencies other than the |
| 98 | +standard library. |
23 | 99 | ```
|
24 |
| -pip install aiodike |
| 100 | +pip install dike |
25 | 101 | ```
|
0 commit comments