Functioning of rate limiter for API requests

  Kiến thức lập trình

I am trying to limit my API calls based on the below limit rates. To achieve this am trying to use ratelimit from python.

  • Per second: 25 requests
  • Per minute: 250 requests
  • Per 30 minutes: 1,000 requests

I am able to use the per second limit based on rate limit as shown below:

from ratelimit import limits, sleep_and_retry

SEC_CALLS = 10
SEC_RATE_LIMIT = 1

@sleep_and_retry
@limits(calls=SEC_CALLS, period=SEC_RATE_LIMIT)
def check_limit_secs():
    print(f'sec time: {time.time()}')
    return

##Function Code:

for i in ID:
   
    check_limit_secs()  
    url = 'https://xxxxxx{}'.format(ID)
    
    headers = {
        'Accept': 'application/json'
    }

    response = requests.get(url, headers=headers)

    # Check the response status
    if response.status_code == 200:
        # Do something with the response data (e.g., print it)
        print(i)
             # print(response.json())
    else:
        # Print an error message if the request was not successful
        print(f"Error: {response.status_code} - {response.text}")

This code stops me from hitting the API after 1 second, but I also want to limit it based on one minute and 30 minutes as well.

FYI: my code hits 25 requests in a second, that means after 10 seconds, my per limit rate would hit. That means my code should stop for about 50 seconds to full fill the per min limit.

Do I have to make multiple calls using ratelimit?

Alternatively use tenacity instead…

from tenacity import retry, stop_after_attempt, wait_random_exponential
@retry(
    reraise=True,
    wait=wait_random_exponential(min=0.1, max=10),
    stop=stop_after_attempt(3),
)
def check_limit(...):
    .... your code

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

Functioning of rate limiter for API requests

I am trying to limit my API calls based on the below limit rates. To achieve this am trying to use ratelimit from python.

  • Per second: 25 requests
  • Per minute: 250 requests
  • Per 30 minutes: 1,000 requests

I am able to use the per second limit based on rate limit as shown below:

from ratelimit import limits, sleep_and_retry

SEC_CALLS = 10
SEC_RATE_LIMIT = 1

@sleep_and_retry
@limits(calls=SEC_CALLS, period=SEC_RATE_LIMIT)
def check_limit_secs():
    print(f'sec time: {time.time()}')
    return

##Function Code:

for i in ID:
   
    check_limit_secs()  
    url = 'https://xxxxxx{}'.format(ID)
    
    headers = {
        'Accept': 'application/json'
    }

    response = requests.get(url, headers=headers)

    # Check the response status
    if response.status_code == 200:
        # Do something with the response data (e.g., print it)
        print(i)
             # print(response.json())
    else:
        # Print an error message if the request was not successful
        print(f"Error: {response.status_code} - {response.text}")

This code stops me from hitting the API after 1 second, but I also want to limit it based on one minute and 30 minutes as well.

FYI: my code hits 25 requests in a second, that means after 10 seconds, my per limit rate would hit. That means my code should stop for about 50 seconds to full fill the per min limit.

Do I have to make multiple calls using ratelimit?

Alternatively use tenacity instead…

from tenacity import retry, stop_after_attempt, wait_random_exponential
@retry(
    reraise=True,
    wait=wait_random_exponential(min=0.1, max=10),
    stop=stop_after_attempt(3),
)
def check_limit(...):
    .... your code

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT