113. Throttling API Requests

Throttling API requests is an essential technique for managing rate limits imposed by APIs and ensuring that you don't overwhelm the API server. Python's asyncio and threading modules can be used effectively to throttle or limit the rate of API requests.


1. Using asyncio for Throttling API Requests

Here's an example of using asyncio.Semaphore to limit the number of concurrent API requests:

Copy

import asyncio
import aiohttp
import time

# Define the maximum number of concurrent requests
CONCURRENT_REQUESTS = 5

async def fetch(session, url):
    async with session.get(url) as response:
        return await response.text()

async def throttled_fetch(sem, session, url):
    async with sem:  # Acquire a semaphore
        print(f"Fetching {url}")
        return await fetch(session, url)

async def main():
    urls = [f"https://jsonplaceholder.typicode.com/posts/{i}" for i in range(1, 21)]
    sem = asyncio.Semaphore(CONCURRENT_REQUESTS)  # Throttle to 5 concurrent requests
    async with aiohttp.ClientSession() as session:
        tasks = [throttled_fetch(sem, session, url) for url in urls]
        results = await asyncio.gather(*tasks)
        print(f"Fetched {len(results)} responses")

start = time.time()
asyncio.run(main())
print(f"Completed in {time.time() - start:.2f} seconds")

Explanation:

  • asyncio.Semaphore limits the number of concurrent requests.

  • aiohttp.ClientSession is used for making asynchronous HTTP requests.

  • Only CONCURRENT_REQUESTS requests run at a time, preventing overload.


2. Using asyncio.sleep for Time-Based Throttling

You can use asyncio.sleep to introduce delays between API calls to comply with rate limits:

Copy

Explanation:

  • asyncio.sleep ensures that there is at least a 1-second delay between consecutive requests.


3. Using threading for Throttling API Requests

Here's an example using a threading.Semaphore to limit the number of threads making API requests:

Copy

Explanation:

  • threading.Semaphore ensures that only CONCURRENT_REQUESTS threads run simultaneously.

  • This approach is useful when working with blocking I/O operations like requests.


4. Using Token Bucket Algorithm with asyncio

A token bucket algorithm can control the rate of API requests precisely:

Copy

Explanation:

  • The TokenBucket class refills tokens at a constant rate (rate).

  • Requests are throttled to match the rate and burst capacity.


5. Rate-Limiting with aiolimiter

The aiolimiter library simplifies rate-limiting.

Copy

Explanation:

  • AsyncLimiter limits the number of requests per second.

  • It automatically handles rate-limiting logic.


Summary of Techniques:

  1. asyncio.Semaphore: Limits the number of concurrent requests.

  2. asyncio.sleep: Adds delays between requests.

  3. threading.Semaphore: Manages concurrent threads.

  4. Token Bucket Algorithm: Precisely controls request rates and bursts.

  5. aiolimiter: Simplifies rate-limiting with a pre-built solution.

Choose the technique that best suits your use case and API constraints!

Last updated