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
-
-
class
aiothrottle.
ThrottledStreamReader
(stream, rate_limit, buffer_limit=65536, loop=None, *args, **kwargs)[source]¶ Bases:
aiohttp.streams.StreamReader
Throttling, flow controlling
aiohttp.streams.StreamReader
foraiohttp.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
-
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.
-
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 receivedParameters: byte_count (int) – the number of bytes to read Returns: the data Return type: bytes