pyrate_limiter.extras.aiohttp_limiter module

class pyrate_limiter.extras.aiohttp_limiter.RateLimitedSession(limiter, name='pyrate', **kwargs)

Bases: object

A thin wrapper around aiohttp.ClientSession that enforces rate limits using a provided Limiter.

Each request acquires a token from the limiter before delegating to the underlying aiohttp session.

__init__(limiter, name='pyrate', **kwargs)

Initialize a new rate-limited session.

Parameters:
  • limiter (Limiter) – Limiter used to control request rate.

  • name (str, optional) – Token/key used by the limiter to bucket this session’s requests.

  • **kwargs – Additional keyword arguments passed to aiohttp.ClientSession.

async close()

Close the underlying aiohttp.ClientSession.

This method ensures that the internal session is properly closed, avoiding ResourceWarning about unclosed transports. It is safe to call even if the session was already closed or if you did not use the context manager.

Return type:

None

async get(*a, **k)

Perform a GET request after acquiring from the limiter.

Parameters:
  • *a – Arguments forwarded to aiohttp.ClientSession.get().

  • **k – Arguments forwarded to aiohttp.ClientSession.get().

Returns:

The response object from the request.

Return type:

aiohttp.ClientResponse

async post(*a, **k)

Perform a POST request after acquiring from the limiter.

Parameters:
  • *a – Arguments forwarded to aiohttp.ClientSession.post().

  • **k – Arguments forwarded to aiohttp.ClientSession.post().

Returns:

The response object from the request.

Return type:

aiohttp.ClientResponse