Skip to main content

A Python rate limiter and throttling utility with async and sync support.

Project description


# twill_throttle

A Python module providing **rate-limiting decorators** for synchronous and asynchronous functions.  
Includes two main classes:

- **`FuncPerMin`** – Limits the number of calls to a single function per minute.
- **`SharedRateLimiter`** – A shared rate limiter across multiple functions, supporting both sync and async.

---

## Features

- Per-function call limiting (`FuncPerMin`)
- Shared call limiting across multiple functions (`SharedRateLimiter`)
- Support for synchronous and asynchronous functions
- Sliding window rate limiting for precise control
- Thread-safe and async-safe locking
- Easy to integrate with any existing function or coroutine

---

## Installation

```bash
pip install twill_throttle

Or just copy the module file into your project.


Usage

Example 1 – Per-function limit

from twill_throttle import FuncPerMin

@FuncPerMin(max_calls_per_minute=3)
def greet(name):
    print(f"Hello, {name}!")

for i in range(5):
    greet(f"User {i+1}")  # Last two calls will wait until the next minute

Example 2 – Shared limit across multiple functions

import asyncio
from twill_throttle import SharedRateLimiter

shared_limiter = SharedRateLimiter(max_calls_per_minute=4)

@shared_limiter
def process_data(data):
    print(f"Processing data: {data}")

@shared_limiter
async def async_task(task_id):
    print(f"Starting async task {task_id}...")
    await asyncio.sleep(0.5)
    print(f"Finished async task {task_id}.")

async def main():
    process_data("File1")
    process_data("File2")
    await asyncio.gather(
        async_task(1),
        async_task(2),
        async_task(3)
    )

asyncio.run(main())

API Reference

FuncPerMin(max_calls_per_minute: int)

Limits calls per minute for one specific function.

  • Parameters:

    • max_calls_per_minute (int) – Maximum allowed calls per minute.
  • Behavior:

    • Resets counter after 60 seconds from the first call.
    • If the limit is reached, blocks execution until the minute resets.

SharedRateLimiter(max_calls_per_minute: int)

Limits calls per minute shared across multiple functions (sync & async).

  • Parameters:

    • max_calls_per_minute (int) – Maximum allowed calls per minute.
  • Behavior:

    • Uses a sliding time window for precision.
    • Waits until enough time passes before allowing a new call.
    • Works for both synchronous and asynchronous functions.

Best Practices

  • Use FuncPerMin when you only need to limit one specific function.
  • Use SharedRateLimiter when multiple functions should share the same limit.
  • Be aware that both limiters block execution until the limit resets. In async contexts, SharedRateLimiter will use non-blocking asyncio.sleep().

License

MIT License – You are free to use, modify, and distribute this code.


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

twill_throttle-1.0.3.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

twill_throttle-1.0.3-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file twill_throttle-1.0.3.tar.gz.

File metadata

  • Download URL: twill_throttle-1.0.3.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.4

File hashes

Hashes for twill_throttle-1.0.3.tar.gz
Algorithm Hash digest
SHA256 d6c961446f7c7a53f82e40e5d31943ace4d6fdad1b696d31023c53874ce48937
MD5 7364cb983e31875485426fa2c69b4196
BLAKE2b-256 b79b029668af3c6eb1a077861d709715324d6e6b798d73dc54b621355e01b0e7

See more details on using hashes here.

File details

Details for the file twill_throttle-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: twill_throttle-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.4

File hashes

Hashes for twill_throttle-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 61b5b0e32cb0618b298f9a4a0cdbe02a56e375454e5c5859bc898a476581c3cb
MD5 8b5a574279cb6dbcac6cc1cfc7321286
BLAKE2b-256 ca1d802a7e02b9c7b9c4ec058de6e3e0532736154599e63571fccfc0da1f6213

See more details on using hashes here.

Supported by

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