https://github.com/sylvainmouquet/concurrency-limiter
Concurrency-Limiter is a Python library that provides a decorator to limit the number of concurrent executions with asyncio.gather
https://github.com/sylvainmouquet/concurrency-limiter
asyncio concurrency decorator gather library limiter python
Last synced: 6 months ago
JSON representation
Concurrency-Limiter is a Python library that provides a decorator to limit the number of concurrent executions with asyncio.gather
- Host: GitHub
- URL: https://github.com/sylvainmouquet/concurrency-limiter
- Owner: sylvainmouquet
- License: mit
- Created: 2024-10-02T08:30:20.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-09T19:38:13.000Z (almost 2 years ago)
- Last Synced: 2026-01-06T10:27:04.097Z (6 months ago)
- Topics: asyncio, concurrency, decorator, gather, library, limiter, python
- Language: Python
- Homepage: https://pypi.org/project/concurrency-limiter
- Size: 101 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Concurrency-Limiter
Concurrency-Limiter is a python decorator to limit the number of concurrent executor in asyncio.gather
### Demonstration:
```python
@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)))
DEBUG concurrency-limiter:__init__.py:61 [RUNNING] func: fetch_data -
DEBUG concurrency-limiter:__init__.py:61 [RUNNING] func: fetch_data -
DEBUG concurrency-limiter:__init__.py:61 [RUNNING] func: fetch_data -
DEBUG concurrency-limiter:__init__.py:61 [RUNNING] func: fetch_data -
DEBUG concurrency-limiter:__init__.py:61 [RUNNING] func: fetch_data -
INFO concurrency-limiter:__init__.py:59 [WAITING] func: fetch_data -
INFO concurrency-limiter:__init__.py:59 [WAITING] func: fetch_data -
INFO concurrency-limiter:__init__.py:59 [WAITING] func: fetch_data -
INFO concurrency-limiter:__init__.py:59 [WAITING] func: fetch_data -
INFO concurrency-limiter:__init__.py:59 [WAITING] func: fetch_data -
DEBUG concurrency-limiter:__init__.py:61 [RUNNING] func: fetch_data -
DEBUG concurrency-limiter:__init__.py:61 [RUNNING] func: fetch_data -
DEBUG concurrency-limiter:__init__.py:61 [RUNNING] func: fetch_data -
DEBUG concurrency-limiter:__init__.py:61 [RUNNING] func: fetch_data -
DEBUG concurrency-limiter:__init__.py:61 [RUNNING] func: fetch_data -
```
## Table of Contents
- [concurrency_limiter](#concurrency_limiter)
- [Table of Contents](#table-of-contents)
- [Description](#description)
- [Installation](#installation)
- [Usage](#usage)
- [License](#license)
- [Contact](#contact)
## 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
```bash
# Install the dependency
pip install concurrency-limiter
uv add concurrency-limiter
poetry add concurrency-limiter
```
## Usage
```python
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](LICENSE) file for more details.
## Contact
For questions, suggestions, or issues related to Concurrency-Limiter, please open an issue on the GitHub repository.