### Install ratelimit Project Source: https://github.com/tomasbasham/ratelimit/blob/master/CONTRIBUTING.rst Steps to clone the ratelimit repository and install its dependencies using pip. ```bash git clone cd ratelimit pip install -r requirements.txt ``` -------------------------------- ### Install ratelimit from GitHub Source: https://github.com/tomasbasham/ratelimit/blob/master/README.rst Installs the latest version of the ratelimit package directly from its GitHub repository by cloning the repository and running the setup script. ```bash git clone https://github.com/tomasbasham/ratelimit cd ratelimit python setup.py install ``` -------------------------------- ### Install ratelimit via PyPI Source: https://github.com/tomasbasham/ratelimit/blob/master/README.rst Installs the ratelimit package using pip, typically by adding it to a requirements.txt file. ```bash pip install ratelimit ``` -------------------------------- ### Basic API Call Rate Limiting Source: https://github.com/tomasbasham/ratelimit/blob/master/README.rst Demonstrates how to use the @limits decorator to restrict a function to a maximum number of calls within a specified period. The example limits API calls to 15 within a 15-minute window. ```python 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 ``` -------------------------------- ### Lint ratelimit Project with Pylint Source: https://github.com/tomasbasham/ratelimit/blob/master/CONTRIBUTING.rst Commands to run Pylint for code quality checks on the ratelimit project and its tests. ```bash pylint ratelimit pylint tests ``` -------------------------------- ### Run Tests with Pytest Source: https://github.com/tomasbasham/ratelimit/blob/master/CONTRIBUTING.rst Command to execute the test suite for the ratelimit project using Pytest. ```bash py.test ``` -------------------------------- ### Project Dependencies Source: https://github.com/tomasbasham/ratelimit/blob/master/requirements.txt Lists the Python package dependencies required for the project, including testing and linting tools. ```python pytest==2.6.4 pytest-cov==2.5.1 pylint==1.7.2 ``` -------------------------------- ### Implementing retry with backoff on RateLimitException (Python) Source: https://github.com/tomasbasham/ratelimit/blob/master/README.rst Combines `@limits` with the `backoff` library's `@on_exception` decorator. If a `RateLimitException` is raised, the function call is automatically retried with an exponential backoff strategy. ```python 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 ``` -------------------------------- ### Implementing sleep and retry with @sleep_and_retry (Python) Source: https://github.com/tomasbasham/ratelimit/blob/master/README.rst Uses the `@sleep_and_retry` decorator alongside `@limits`. When the rate limit is hit, the current thread pauses until the limit period resets, and then the function call is automatically retried. ```python 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 ``` -------------------------------- ### Rate Limiting with Exponential Backoff Retry Source: https://github.com/tomasbasham/ratelimit/blob/master/README.rst Combines the @limits decorator with the @on_exception decorator from the 'backoff' library to automatically retry function calls that exceed rate limits using an exponential backoff strategy. It attempts a maximum of 8 retries. ```python 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 ``` -------------------------------- ### Rate Limiting with Sleep and Retry Source: https://github.com/tomasbasham/ratelimit/blob/master/README.rst Utilizes the @sleep_and_retry decorator along with @limits to pause the current thread until the rate limit period has elapsed before retrying the function. This ensures successful execution at the cost of thread halting. ```python 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 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.