pak.io.streams

Asynchronous data streams.

class ByteStreamReader(data=b'')[source]

Bases: object

An asyncio.StreamReader which reads from predetermined data.

Note

While this technically does not inherit from asyncio.StreamReader, it has the same API and semantics. Thus it is perfectly usable for e.g. io.Connection.

Parameters:

data (bytes-like) – The data to read from.

async read(n=-1)[source]

Reads up to n bytes.

Parameters:

n (int) –

The number of bytes to read.

If -1, then read until EOF.

Returns:

The data read from the stream.

Return type:

bytes

async readline()[source]

Reads until the next newline.

If EOF is reached before the next newline, then partial data is returned.

Returns:

The data read from the stream.

The newline will be included in the data.

Return type:

bytes

async readexactly(n)[source]

Reads exactly n bytes.

Parameters:

n (int) – The exact number of bytes to read.

Returns:

The data read from the stream.

Return type:

bytes

Raises:

asyncio.IncompleteReadError – If n bytes cannot be read. The partial attribute will contain the partially read data.

async readuntil(separator=b'\n')[source]

Reads until a separator is found.

Parameters:

separator (bytes or tuple of bytes) –

If bytes, then the separator to read until.

If a tuple, then the collection of possible separators to read until. The separator which results in the least amount of data being read will be the one utilized.

Returns:

The data read from the stream.

The appropriate separator will be included in the data.

Return type:

bytes

Raises:
  • ValueError – If the separators don’t all contain at least one byte.

  • asyncio.IncompleteReadError – If no separator can be found. The partial attribute will contain the partially read data, potentially including part of a separator.

at_eof()[source]

Gets whether the stream has ended.

Returns:

Whether the stream has ended.

Return type:

bool

class ByteStreamWriter[source]

Bases: object

An asyncio.StreamWriter which writes to an internal buffer.

Note

While this technically does not inherit from asyncio.StreamWriter, it has the same API and semantics. Thus it is perfectly usable for e.g. io.Connection.

property written_data

The data that has been written.

Return type:

bytes

write(data)[source]

Writes data to the internal buffer.

This method should be used along with the drain() method.

Parameters:

data (bytes-like) – The data to write.

writelines(data)[source]

Writes an iterable of bytes to the internal buffer.

This method should be used along with the drain() method.

Parameters:

data (iterable of bytes-like) – The iterable of bytes to write.

async drain()[source]

Waits until it is appropriate to resume writing to the ByteStreamWriter.

close()[source]

Closes the ByteStreamWriter.

This method should be used along with the wait_closed() method.

is_closing()[source]

Gets whether the ByteStreamWriter is closed or in the process of closing.

Returns:

Whether the ByteStreamWriter is closed or in the process of closing.

Return type:

bool

async wait_closed()[source]

Waits until the ByteStreamWriter is closed.