Skip to main content

Python asyncio tools for web service resilience.

Project description


Python asyncio tools for web service resilience

Release Status CI Status codecov


Concurrency limiting for asynchronous functions

The @limit_jobs decorator allows to limit the number of concurrent excecutions of a coroutine function. This can be useful for limiting queueing times or for limiting the load put onto backend services.

Example with an external web request using the httpx library:

import asyncio
import dike
import httpx

async def web_request():
    """Sends a slow web request"""
    async with httpx.AsyncClient() as client:
        response = await client.get("")
    return response

async def main():
    # Send three requests at the same time
    call1 = web_request()
    call2 = web_request()
    call3 = web_request()
    responses = await asyncio.gather(call1, call2, call3, return_exceptions=True)
    # Print the responses
    for r in responses:
        if isinstance(r, dike.TooManyCalls):
            print("too many calls")

The output shows that the first two requests succeed. The third one hits the concurrency limit and a TooManyCalls exception is returned:

<Response [200 OK]>
<Response [200 OK]>
too many calls

Mini-batching for asynchronous function calls

The @batch decorator groups function calls into batches and only calls the wrapped function with the aggregated input.

This is useful if the function scales well with the size of the input arguments but you're getting the input data in smaller bits, e.g. as individual HTTP requests.

The arguments can be batched together as a Python list or optionally also as numpy array.


import asyncio
import dike

@dike.batch(target_batch_size=3, max_waiting_time=10)
async def add_args(arg1, arg2):
    """Elementwise sum of the values in arg1 and arg2"""
    print(f"arg1: {arg1}")
    print(f"arg2: {arg2}")
    return [a1 + a2 for a1, a2 in zip(arg1, arg2)]

async def main():
    result = await asyncio.gather(
        add_args([0], [1]),
        add_args([1], [1]),
        add_args([2, 3], [1, 1]),

    print(f"Result: {result}")


arg1: [0, 1, 2, 3]
arg2: [1, 1, 1, 1]
Result: [[1], [2], [3, 4]]


Simply install from pypi. The library is pure Python without any dependencies other than the standard library.

pip install dike

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dike-0.3.1.tar.gz (14.9 kB view hashes)

Uploaded Source

Built Distribution

dike-0.3.1-py3-none-any.whl (11.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page