asynciolimiter

1.2.0 · active · verified Thu Apr 16

asynciolimiter is a simple yet efficient Python library providing various rate limiting algorithms for AsyncIO applications. It offers different limiter flavors like `Limiter`, `LeakyBucketLimiter`, and `StrictLimiter` to control the rate of asynchronous operations. The current version is 1.2.0, actively maintained with regular releases addressing features and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the basic `Limiter` to constrain the execution rate of asynchronous tasks. It creates a limiter allowing 2 requests per second (10 requests over 5 seconds) and then schedules 10 tasks, each waiting for the limiter before proceeding.

import asyncio
from asynciolimiter import Limiter

# Limit to 2 requests per second (10 requests per 5 seconds)
rate_limiter = Limiter(10/5)

async def request_task(task_id):
    await rate_limiter.wait() # Wait for a slot to be available
    print(f"Task {task_id}: Request sent at {asyncio.get_event_loop().time():.2f}")

async def main():
    print("Starting rate-limited tasks...")
    tasks = [request_task(i) for i in range(10)]
    await asyncio.gather(*tasks)
    print("All tasks completed.")

if __name__ == "__main__":
    asyncio.run(main())

view raw JSON →