Streaming API

aiohttp uses streams for retrieving BODIES: aiohttp.web.Request.content and aiohttp.ClientResponse.content are properties with stream API.

class aiohttp.StreamReader[source]

The reader from incoming stream.

User should never instantiate streams manually but use existing aiohttp.web.Request.content and aiohttp.ClientResponse.content properties for accessing raw BODY data.

Reading Methods

coroutine StreamReader.read(n=-1)[source]

Read up to n bytes. If n is not provided, or set to -1, read until EOF and return all read bytes.

If the EOF was received and the internal buffer is empty, return an empty bytes object.

Parameters:n (int) – how many bytes to read, -1 for the whole stream.
Return bytes:the given data
coroutine StreamReader.readany()[source]

Read next data portion for the stream.

Returns immediately if internal buffer has a data.

Return bytes:the given data
coroutine StreamReader.readexactly(n)[source]

Read exactly n bytes.

Raise an asyncio.IncompleteReadError if the end of the stream is reached before n can be read, the asyncio.IncompleteReadError.partial attribute of the exception contains the partial read bytes.

Parameters:n (int) – how many bytes to read.
Return bytes:the given data
coroutine StreamReader.readline()[source]

Read one line, where “line” is a sequence of bytes ending with \n.

If EOF is received, and \n was not found, the method will return the partial read bytes.

If the EOF was received and the internal buffer is empty, return an empty bytes object.

Return bytes:the given line

Asynchronous Iteration Support

Stream reader supports asynchronous iteration over BODY.

By default it iterates over lines:

async for line in response.content:
    print(line)

Also there are methods for iterating over data chunks with maximum size limit and over any available data.

async-for StreamReader.iter_chunked(n)

Iterates over data chunks with maximum size limit:

async for data in response.content.iter_chunked(1024):
    print(data)
async-for StreamReader.iter_any(n)

Iterates over data chunks in order of intaking them into the stream:

async for data in response.content.iter_any():
    print(data)

Helpers

StreamReader.exception()[source]

Get the exception occurred on data reading.

aiohttp.is_eof()

Return True if EOF was reached.

Internal buffer may be not empty at the moment.

StreamReader.at_eof()[source]

Return True if the buffer is empty and EOF was reached.

StreamReader.read_nowait(n=None)[source]

Returns data from internal buffer if any, empty bytes object otherwise.

Raises RuntimeError if other coroutine is waiting for stream.

Parameters:n (int) – how many bytes to read, -1 for the whole internal buffer.
Return bytes:the given data
StreamReader.unread_data(data)[source]

Rollback reading some data from stream, inserting it to buffer head.

Parameters:data (bytes) – data to push back into the stream.

Warning

The method doesn’t wake up waiters.

E.g. read() will not be resumed.

coroutine aiohttp.wait_eof()

Wait for EOF. The given data may be accessible by upcoming read calls.

blog comments powered by Disqus