pak.io.streams¶
Asynchronous data streams.
- class ByteStreamReader(data=b'')[source]¶
Bases:
objectAn
asyncio.StreamReaderwhich 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
nbytes.- 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
nbytes.- Parameters:
n (
int) – The exact number of bytes to read.- Returns:
The data read from the stream.
- Return type:
bytes- Raises:
asyncio.IncompleteReadError – If
nbytes cannot be read. Thepartialattribute will contain the partially read data.
- async readuntil(separator=b'\n')[source]¶
Reads until a separator is found.
- Parameters:
separator (
bytesortupleofbytes) –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
partialattribute will contain the partially read data, potentially including part of a separator.
- class ByteStreamWriter[source]¶
Bases:
objectAn
asyncio.StreamWriterwhich 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
ByteStreamWriteris closed or in the process of closing.- Returns:
Whether the
ByteStreamWriteris closed or in the process of closing.- Return type:
bool
- async wait_closed()[source]¶
Waits until the
ByteStreamWriteris closed.