Skip to main content

mThrottle

A simple and generic rate limiter to prevent too many function calls or http requests.

When making large number of HTTP requests, its essential to respect the API limits of a server.

The Throttle class ensures you do not exceed these limits.

Python version: >= 3.7

Install with PIP

pip install mthrottle

Usage

Throttle takes two arguments - a dictionary configuration and an integer maxPenaltyCount.

The max_penalty_count is the maximum number of 429 HTTP error code, allowed to be returned during a session. Exceeding this limit will result in a runtime error.

The configuration is a dictionary which defines the limits for each endpoint. Here, RPS stands for Requests Per Second and RPM stands for Requests Per Minute. Limits are set for each end point.

Configuration must have a default key which defines the default limits.

To rate limit, run th.check method before a request or function call.

from mthrottle import Throttle

config = {
    'default': {
        'rps': 3,
        'rpm': 150
    }
    'search': {
        'rps': 20
        'rpm': 500
    }
}

maxPenaltyCount = 10

th = Throttle(config, maxPenaltyCount)

for i in range(200):
    th.check()
    print(i, flush=True, end='\r' * 3)

th.check takes a key argument, matching one of the key values in config. If key is not specified, default is used.

Example using Throttle.penalize

Throttle.penalize returns a boolean and must be called when a 429 HTTP status code is returned. It returns True when maxPenaltyCount is exceeded.

from mthrottle import Throttle
import requests

config = {
    'run': {
        'rpm': 3
    }
}

maxPenaltyCount = 10

th = Throttle(config, maxPenaltyCount)

for i in range(200):
    th.check(key='run')
    r = requests.get('https://google.com')

    if r.status_code == 429:
        if th.penalize():
            raise RuntimeError('Too many API rate limit warnings.')

Metadata

Release files for mthrottle 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mthrottle 0.0.2
File Size Uploaded Provenance Actions
mthrottle-0.0.2.tar.gz 16.2 kB Without provenance Details for mthrottle-0.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mthrottle 0.0.2
File Interpreter ABI Platform Actions
mthrottle-0.0.2-py3-none-any.whl Python 3 none any Details for mthrottle-0.0.2-py3-none-any.whl

Total release size: 31.6 kB

Release files / mthrottle-0.0.2.tar.gz

Download URL mthrottle-0.0.2.tar.gz
Size 16.2 kB
Tags Source
SHA-256 checksum
How to use checksums
c7e771e9fe02c9716e3d825c8a3f97859af693383c070175dd7a69e1a474115a
BLAKE2b-256 checksum
How to use checksums
ae2ebf9010dc1b2f97ee83fa6f2dd82039dc7893463594218f20c83b3dc8892d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / mthrottle-0.0.2-py3-none-any.whl

Download URL mthrottle-0.0.2-py3-none-any.whl
Size 15.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fc170ff29ec2e76ef57446328720e3c586dd82d9a83ccd79640c7b0f017b3a32
BLAKE2b-256 checksum
How to use checksums
24680a695fc6fa882a4533401b79454fe5291dd27e7a6b6806cb25b883302171
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Security for mthrottle 0.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page