mthrottle


Namemthrottle JSON
Version 0.0.2 PyPI version JSON
download
home_page
SummaryA simple and generic rate limiter to prevent too many function calls or http requests.
upload_time2023-12-06 18:18:13
maintainer
docs_urlNone
authorBenny Thadikaran
requires_python>=3.7
license
keywords rate-limiter throttle throttle-requests throttler
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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.

```python
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.

```python
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.')
```

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "mthrottle",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "rate-limiter,throttle,throttle-requests,throttler",
    "author": "Benny Thadikaran",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/ae/2e/bf9010dc1b2f97ee83fa6f2dd82039dc7893463594218f20c83b3dc8892d/mthrottle-0.0.2.tar.gz",
    "platform": null,
    "description": "# mThrottle\n\nA simple and generic rate limiter to prevent too many function calls or http requests.\n\nWhen making large number of HTTP requests, its essential to respect the API limits of a server.\n\nThe Throttle class ensures you do not exceed these limits.\n\nPython version: >= 3.7\n\n## Install with PIP\n\n```\npip install mthrottle\n```\n\n## Usage\n\nThrottle takes two arguments - a dictionary `configuration` and an integer `maxPenaltyCount`.\n\nThe `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.\n\nThe `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.\n\nConfiguration must have a `default` key which defines the default limits.\n\nTo rate limit, run `th.check` method before a request or function call.\n\n```python\nfrom mthrottle import Throttle\n\nconfig = {\n    'default': {\n        'rps': 3,\n        'rpm': 150\n    }\n    'search': {\n        'rps': 20\n        'rpm': 500\n    }\n}\n\nmaxPenaltyCount = 10\n\nth = Throttle(config, maxPenaltyCount)\n\nfor i in range(200):\n    th.check()\n    print(i, flush=True, end='\\r' * 3)\n```\n\n`th.check` takes a `key` argument, matching one of the key values in config. If key is not specified, `default` is used.\n\n### Example using Throttle.penalize\n\n`Throttle.penalize` returns a boolean and must be called when a 429 HTTP status code is returned. It returns True when `maxPenaltyCount` is exceeded.\n\n```python\nfrom mthrottle import Throttle\nimport requests\n\nconfig = {\n    'run': {\n        'rpm': 3\n    }\n}\n\nmaxPenaltyCount = 10\n\nth = Throttle(config, maxPenaltyCount)\n\nfor i in range(200):\n    th.check(key='run')\n    r = requests.get('https://google.com')\n\n    if r.status_code == 429:\n        if th.penalize():\n            raise RuntimeError('Too many API rate limit warnings.')\n```\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "A simple and generic rate limiter to prevent too many function calls or http requests.",
    "version": "0.0.2",
    "project_urls": {
        "Bug Tracker": "https://github.com/BennyThadikaran/mThrottle/issues",
        "Homepage": "https://github.com/BennyThadikaran/mThrottle"
    },
    "split_keywords": [
        "rate-limiter",
        "throttle",
        "throttle-requests",
        "throttler"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "24680a695fc6fa882a4533401b79454fe5291dd27e7a6b6806cb25b883302171",
                "md5": "54277152c33b70d2921b1b9371a63b68",
                "sha256": "fc170ff29ec2e76ef57446328720e3c586dd82d9a83ccd79640c7b0f017b3a32"
            },
            "downloads": -1,
            "filename": "mthrottle-0.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "54277152c33b70d2921b1b9371a63b68",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 15377,
            "upload_time": "2023-12-06T18:18:11",
            "upload_time_iso_8601": "2023-12-06T18:18:11.775632Z",
            "url": "https://files.pythonhosted.org/packages/24/68/0a695fc6fa882a4533401b79454fe5291dd27e7a6b6806cb25b883302171/mthrottle-0.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ae2ebf9010dc1b2f97ee83fa6f2dd82039dc7893463594218f20c83b3dc8892d",
                "md5": "2758b2bca08ce34d68b60196d104a7bb",
                "sha256": "c7e771e9fe02c9716e3d825c8a3f97859af693383c070175dd7a69e1a474115a"
            },
            "downloads": -1,
            "filename": "mthrottle-0.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "2758b2bca08ce34d68b60196d104a7bb",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 16238,
            "upload_time": "2023-12-06T18:18:13",
            "upload_time_iso_8601": "2023-12-06T18:18:13.647457Z",
            "url": "https://files.pythonhosted.org/packages/ae/2e/bf9010dc1b2f97ee83fa6f2dd82039dc7893463594218f20c83b3dc8892d/mthrottle-0.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-06 18:18:13",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "BennyThadikaran",
    "github_project": "mThrottle",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "mthrottle"
}
        
Elapsed time: 0.14574s