Throttler

1.2.3 · active · verified Wed Apr 15

Throttler is a zero-dependency Python package designed for easy rate limiting and concurrency control, with robust support for asyncio. It provides flexible context managers and decorators to manage the frequency and simultaneity of operations. The library is actively maintained, with version 1.2.3 being the latest, and receives updates to support newer Python versions and improve performance.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates using the `Throttler` context manager to rate-limit asynchronous tasks to 3 executions per second. The `asyncio.run()` function is used to execute the main asynchronous function.

import asyncio
from throttler import Throttler
import time

async def limited_task(task_id, throttler):
    async with throttler:
        print(f"{time.time():.2f}: Task {task_id} executing...")

async def main():
    # Limit to 3 calls per second
    rate_limiter = Throttler(rate_limit=3, period=1.0)
    tasks = [limited_task(i, rate_limiter) for i in range(10)]
    await asyncio.gather(*tasks)

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

view raw JSON →