Setting API rate limit on Python with ratelimit library - TagMerge
4Setting API rate limit on Python with ratelimit librarySetting API rate limit on Python with ratelimit library

Setting API rate limit on Python with ratelimit library

Asked 1 years ago
1
4 answers

It throughs exception for me while I try to make more than 5 iterations of call on API, getting exception after 5 calls,

ratelimit.exception.RateLimitException: too many calls

Fullcode:

from flask import Flask
from ratelimit import limits

max_hit = 5
period = 300

@limits(calls=max_hit, period=period)
def StashNotes():
    return "sany"

app = Flask(__name__)

@app.route("/")
def hello_world():
    return StashNotes()

Source: link

0

To install ratelimit, simply:
$ pip install ratelimit
Installing the latest version from Github:
$ git clone https://github.com/tomasbasham/ratelimit
$ cd ratelimit
$ python setup.py install
To use this package simply decorate any function that makes an API call:
from ratelimit import limits

import requests

FIFTEEN_MINUTES = 900

@limits(calls=15, period=FIFTEEN_MINUTES)
def call_api(url):
    response = requests.get(url)

    if response.status_code != 200:
        raise Exception('API response: {}'.format(response.status_code))
    return response
If a decorated function is called more times than that allowed within the specified time period then a ratelimit.RateLimitException is raised. This may be used to implement a retry strategy such as an expoential backoff
from ratelimit import limits, RateLimitException
from backoff import on_exception, expo

import requests

FIFTEEN_MINUTES = 900

@on_exception(expo, RateLimitException, max_tries=8)
@limits(calls=15, period=FIFTEEN_MINUTES)
def call_api(url):
    response = requests.get(url)

    if response.status_code != 200:
        raise Exception('API response: {}'.format(response.status_code))
    return response
Alternatively to cause the current thread to sleep until the specified time period has ellapsed and then retry the function use the sleep_and_retry decorator. This ensures that every function invocation is successful at the cost of halting the thread.
from ratelimit import limits, sleep_and_retry

import requests

FIFTEEN_MINUTES = 900

@sleep_and_retry
@limits(calls=15, period=FIFTEEN_MINUTES)
def call_api(url):
    response = requests.get(url)

    if response.status_code != 200:
        raise Exception('API response: {}'.format(response.status_code))
    return response

Source: link

0

from ratelimiter import RateLimiter

@RateLimiter(max_calls=10, period=1)
def do_something():
    pass
import requests

def access_rate_limited_api(count):
    resp = requests.get('http://192.168.1.2:8000/knockknock')
    print(f"{count}.{resp.text}")    

for i in range(60):
    access_rate_limited_api(i)
import requests
from ratelimit import limits, RateLimitException, sleep_and_retry

ONE_MINUTE = 60
MAX_CALLS_PER_MINUTE = 30

@sleep_and_retry
@limits(calls=MAX_CALLS_PER_MINUTE, period=ONE_MINUTE)
def access_rate_limited_api(count):
    resp = requests.get('http://192.168.1.2:8000/knockknock')
    print(f"{count}.{resp.text}")    

for i in range(60):
    access_rate_limited_api(i)
192.168.1.4 - - [16/Aug/2021:16:48:06 -0700] "GET /knockknock HTTP/1.1" 200 12 "-" "python-requests/2.25.1"
192.168.1.4 - - [16/Aug/2021:16:48:06 -0700] "GET /knockknock HTTP/1.1" 200 12 "-" "python-requests/2.25.1"
...
192.168.1.4 - - [16/Aug/2021:16:49:06 -0700] "GET /knockknock HTTP/1.1" 200 12 "-" "python-requests/2.25.1"
192.168.1.4 - - [16/Aug/2021:16:49:06 -0700] "GET /knockknock HTTP/1.1" 200 12 "-" "python-requests/2.25.1
import requests
from ratelimit import limits, RateLimitException, sleep_and_retry
from concurrent.futures import ThreadPoolExecutor as PoolExecutor

ONE_MINUTE = 60
MAX_CALLS_PER_MINUTE = 30

@sleep_and_retry
@limits(calls=MAX_CALLS_PER_MINUTE, period=ONE_MINUTE)
def access_rate_limited_api(count):
    resp = requests.get('http://192.168.1.2:8000/knockknock')
    print(f"{count}.{resp.text}")    

with PoolExecutor(max_workers=3) as executor:
    for _ in executor.map(access_rate_limited_api, range(60)):
        pass

Source: link

0

Add this line to your application's requirements.txt:
ratelimit
And then execute:
$ pip install -r requirements.txt
Or install it yourself:
$ pip install ratelimit
Installing the latest version from Github:
$ git clone https://github.com/tomasbasham/ratelimit
$ cd ratelimit
$ python setup.py install
To use this package simply decorate any function that makes an API call:
from ratelimit import limits

import requests

FIFTEEN_MINUTES = 900

@limits(calls=15, period=FIFTEEN_MINUTES)
def call_api(url):
    response = requests.get(url)

    if response.status_code != 200:
        raise Exception('API response: {}'.format(response.status_code))
    return response

Source: link

Recent Questions on python

    Programming Languages