Skip to main content

Limit the number of concurrent executions in asyncio.gather

Project description

Concurrency-Limiter

Concurrency-Limiter is a python decorator to limit the number of concurrent executor in asyncio.gather

Demonstration:

@pytest.mark.asyncio
async def test_concurrency_limiter():
    @concurrency_limiter(max_concurrent=5)
    async def fetch_data(id):
        logger.info(f"Fetch data: {id}")
        await asyncio.sleep(1)

    await asyncio.gather(*(fetch_data(i) for i in range(10)))

INFO     concurrency-limiter:__init__.py:61 run function: fetch_data timestamp: 2024-10-02 09:36:42.246222+00:00 <asyncio.locks.BoundedSemaphore object at 0x1026c8a50 [unlocked, value:4]>
INFO     concurrency-limiter:__init__.py:61 run function: fetch_data timestamp: 2024-10-02 09:36:42.246624+00:00 <asyncio.locks.BoundedSemaphore object at 0x1026c8a50 [unlocked, value:3]>
INFO     concurrency-limiter:__init__.py:61 run function: fetch_data timestamp: 2024-10-02 09:36:42.246735+00:00 <asyncio.locks.BoundedSemaphore object at 0x1026c8a50 [unlocked, value:2]>
INFO     concurrency-limiter:__init__.py:61 run function: fetch_data timestamp: 2024-10-02 09:36:42.246819+00:00 <asyncio.locks.BoundedSemaphore object at 0x1026c8a50 [unlocked, value:1]>
INFO     concurrency-limiter:__init__.py:61 run function: fetch_data timestamp: 2024-10-02 09:36:42.246895+00:00 <asyncio.locks.BoundedSemaphore object at 0x1026c8a50 [locked]>
INFO     concurrency-limiter:__init__.py:57 waiting function: fetch_data timestamp: 2024-10-02 09:36:42.246964+00:00 <asyncio.locks.BoundedSemaphore object at 0x1026c8a50 [locked]>
INFO     concurrency-limiter:__init__.py:57 waiting function: fetch_data timestamp: 2024-10-02 09:36:42.246996+00:00 <asyncio.locks.BoundedSemaphore object at 0x1026c8a50 [locked, waiters:1]>
INFO     concurrency-limiter:__init__.py:57 waiting function: fetch_data timestamp: 2024-10-02 09:36:42.247022+00:00 <asyncio.locks.BoundedSemaphore object at 0x1026c8a50 [locked, waiters:2]>
INFO     concurrency-limiter:__init__.py:57 waiting function: fetch_data timestamp: 2024-10-02 09:36:42.247049+00:00 <asyncio.locks.BoundedSemaphore object at 0x1026c8a50 [locked, waiters:3]>
INFO     concurrency-limiter:__init__.py:57 waiting function: fetch_data timestamp: 2024-10-02 09:36:42.247073+00:00 <asyncio.locks.BoundedSemaphore object at 0x1026c8a50 [locked, waiters:4]>
INFO     concurrency-limiter:__init__.py:61 run function: fetch_data timestamp: 2024-10-02 09:36:43.248582+00:00 <asyncio.locks.BoundedSemaphore object at 0x1026c8a50 [locked, waiters:4]>
INFO     concurrency-limiter:__init__.py:61 run function: fetch_data timestamp: 2024-10-02 09:36:43.250872+00:00 <asyncio.locks.BoundedSemaphore object at 0x1026c8a50 [locked, waiters:3]>
INFO     concurrency-limiter:__init__.py:61 run function: fetch_data timestamp: 2024-10-02 09:36:43.251272+00:00 <asyncio.locks.BoundedSemaphore object at 0x1026c8a50 [locked, waiters:2]>
INFO     concurrency-limiter:__init__.py:61 run function: fetch_data timestamp: 2024-10-02 09:36:43.251523+00:00 <asyncio.locks.BoundedSemaphore object at 0x1026c8a50 [locked, waiters:1]>
INFO     concurrency-limiter:__init__.py:61 run function: fetch_data timestamp: 2024-10-02 09:36:43.251759+00:00 <asyncio.locks.BoundedSemaphore object at 0x1026c8a50 [locked]>
PASSED  

Table of Contents

Description

Concurrency-Limiter is a Python library that provides a decorator to limit the number of concurrent executions in asyncio.gather. This ensures that your asynchronous tasks do not overwhelm system resources, allowing for better control over concurrency and improved performance in applications that rely on asynchronous operations.

Installation

# Install the dependency
pip install concurrency-limiter
uv add concurrency-limiter
poetry add concurrency-limiter

Usage

from concurrency_limiter import concurrency_limiter
import asyncio
import random

# List of flowers for our examples
flowers = ["Rose", "Tulip", "Sunflower", "Daisy", "Lily"]

# Asynchronous function example to plant a flower
@concurrency_limiter(max_concurrent=3)
async def plant_flower():
    flower = random.choice(flowers)
    print(f"Attempting to plant a {flower}")
    await asyncio.sleep(0.1)  # Simulating planting time
    if random.random() < 0.8:  # 80% chance of failure
        raise Exception(f"The {flower} didn't take root")
    return f"{flower} planted successfully"

# Start of Selection
if __name__ == "__main__":
    asyncio.run(asyncio.gather(*(plant_flower() for i in range(5))))
# End of Selection

License

Concurrency-Limiter is released under the MIT License. See the LICENSE file for more details.

Contact

For questions, suggestions, or issues related to Concurrency-Limiter, please open an issue on the GitHub repository.

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

concurrency_limiter-0.0.1.tar.gz (41.9 kB view details)

Uploaded Source

Built Distribution

concurrency_limiter-0.0.1-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file concurrency_limiter-0.0.1.tar.gz.

File metadata

  • Download URL: concurrency_limiter-0.0.1.tar.gz
  • Upload date:
  • Size: 41.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for concurrency_limiter-0.0.1.tar.gz
Algorithm Hash digest
SHA256 3b71a404f1d33adf16056a99a3a2caeca1ae0540b1340bb91b7e6212056dc5c2
MD5 75be6313365cd8d48dc4d56224540fe7
BLAKE2b-256 a3cdb55d50334678b23f586b7717abdc6d2fd1f5dc3b4e7ba456b792b92b576b

See more details on using hashes here.

File details

Details for the file concurrency_limiter-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for concurrency_limiter-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6bc8adda12bf77af8b96a26486fadf5a401929d03e303eab53b2a84d1b262738
MD5 82355fa3d7417860f213f6d2e608da05
BLAKE2b-256 658e78ae1368b1791e83f64f61cfc112da58b7ac83309e9ed60e0c6c674b821f

See more details on using hashes here.

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