aiothrottle package

Module contents

Providing classes for limiting data rates of asyncio sockets

class aiothrottle.Throttle(limit, loop=None)[source]

Bases: object

Throttle for IO operations

As soon as an IO action is registered using add_io(), time_left() returns the seconds to wail until [byte count] / [time passed] = [rate limit]. After that, reset_io() has to be called to measure the new rate.

Parameters:limit (int) – the limit in bytes to read/write per second
Raises:ValueError: invalid rate given
add_io(byte_count)[source]

registers a number of bytes read/written

Parameters:byte_count (int) – number of bytes to add to the current rate
current_rate()[source]

returns the current rate, measured since reset_io()

In case the time since the last reset is too short, this returns -1.

Returns:the current rate in bytes per second
Return type:float
limit
Returns:the limit in bytes to read/write per second
Return type:int
reset_io()[source]

resets the registered IO actions

time_left()[source]

returns the number of seconds left until the rate limit is reached

Returns:seconds left until the rate limit is reached
Return type:float
wait_remaining()[source]

waits until the rate limit is reached

within_limit()[source]

returns the current limitation state

Returns:True if the current rate is equal or below the limit rate
Return type:bool
class aiothrottle.ThrottledStreamReader(stream, rate_limit, buffer_limit=65536, loop=None, *args, **kwargs)[source]

Bases: aiohttp.streams.StreamReader

Throttling, flow controlling aiohttp.streams.StreamReader for aiohttp.request()

Usage:
>>> import functools
>>> import aiohttp
>>> import aiothrottle
>>> kbps = 200
>>> partial = functools.partial(
>>>     aiothrottle.ThrottledStreamReader, rate_limit=kbps * 1024)
>>> aiohttp.client_reqrep.ClientResponse.flow_control_class = partial
Parameters:
  • stream (aiohttp.parsers.StreamParser) – the base stream
  • rate_limit (int) – the rate limit in bytes per second
  • buffer_limit (int) – the internal buffer limit in bytes
  • loop (asyncio.BaseEventLoop) – the asyncio event loop
  • args (tuple) – arguments passed through to StreamReader
  • kwargs (dict) – keyword arguments passed through to StreamReader
feed_data(data, _=0)[source]

Feeds data into the internal buffer

limit_rate(limit)[source]

Sets the rate limit of this response

Parameters:limit – the limit in bytes to read/write per second

New in version 0.1.1.

rate_limit
Returns:the current rate limit
Return type:int

New in version 0.1.2.

read(byte_count=-1)[source]

Reads at most the requested number of bytes from the internal buffer

Parameters:byte_count (int) – the number of bytes to read
Returns:the data
Return type:bytes
readany()[source]

Reads the bytes next received from the internal buffer

Returns:the data
Return type:bytes
readexactly(byte_count)[source]

Reads the requested number of bytes from the internal buffer

This raises asyncio.IncompleteReadError if the stream ended before enough bytes were received

Parameters:byte_count (int) – the number of bytes to read
Returns:the data
Return type:bytes
readline()[source]

Reads bytes from the internal buffer until \n is found

Returns:the data
Return type:bytes
throttling
Returns:wether the connection is being throttled
Return type:bool

New in version 0.1.2.

unlimit_rate()[source]

Unlimits the rate of this response

New in version 0.1.1.

aiothrottle.limit_rate(limit)[source]

Limits the rate of all subsequent aiohttp requests

Parameters:limit – the limit in bytes to read/write per second

New in version 0.1.1.

aiothrottle.unlimit_rate()[source]

Unlimits the rate of all subsequent aiohttp requests

New in version 0.1.1.