Streams (generic high-level API)

Streams provide a generic, high-level API to work with connection-oriented transports using file-like, blocking and flow-controlled API.

Utility classes

class gruvi.StreamReader(on_buffer_size_change=None, timeout=None)

Bases: io.BufferedIOBase

A stream reader.

This is a utility class that is used to implement a blocking reader interface on top of a memory buffer.

A stream reader always operates on bytes instances. To create a reader that works on unicode strings, you can wrap it with a io.TextIOWrapper.

buffer_size

Return the amount of bytes currently in the buffer.

eof

Return whether the stream is currently at end-of-file.

closed

Return whether the stream is currently at end-of-file.

feed(data)

Add data to the buffer.

feed_eof()

Set the EOF condition.

feed_error(exc)

Set the error condition to exc.

read(size=-1)

Read up to size bytes.

This function reads from the buffer multiple times until the requested number of bytes can be satisfied. This means that this function may block to wait for more data, even if some data is available. The only time a short read is returned, is on EOF or error.

If size is not specified or negative, read until EOF.

This method is a switchpoint.

read1(size=-1)

Read up to size bytes.

This function reads from the buffer only once. It is useful in case you need to read a large input, and want to do so efficiently. If size is big enough, then this method will return the chunks passed into the memory buffer verbatim without any copying or slicing.

This method is a switchpoint.

readline(limit=-1, delim='n')

Read a single line.

If EOF is reached before a full line can be read, a partial line is returned. If limit is specified, at most this many bytes will be read.

This method is a switchpoint.

readlines(hint=-1)

Read lines until EOF, and return them as a list.

If hint is specified, then stop reading lines as soon as the total size of all lines exceeds hint.

This method is a switchpoint.

class gruvi.StreamWriter(transport, protocol)

Bases: io.BufferedIOBase

A stream writer.

This class implements flow control in the write direction for a transport/protocol pair.

A stream writer always operates on bytes instances. To create a writer that works on unicode strings, you can wrap it with a io.TextIOWrapper.

write(data)

Write data to the transport.

This method just submits the write to the transport. The transport itself will issue the write at a later time. If there’s an error once the write is issued, it an exception will be set on the transport, that will be re-raised by future write() and other method.

This method may block if the protocol is currently blocked, i.e. if the protocol’s pause_writing() method was called by the transport. In this case, the write block until the protocol’s resume_writing() method is called.

This method is a switchpoint.

writelines(seq)

Write the elements of the sequence seq to the transport.

This method implements flow control as described in write().

This method is a switchpoint.

write_eof()

Close the write direction of the transport.

This method implements flow control as described in write(). The EOF will only be written when the protocol is not paused.

This method is a switchpoint.

close()

Close the transport.

This method will wait until all outstanding data in the transport is flushed, and the transport is closed by the event loop.

This method is a switchpoint.

class gruvi.ReadWriteStream(reader, writer)

Bases: io.BufferedIOBase

A read-write stream.

This is an adapter class that creates a read-write stream on top of a StreamReader and a StreamWriter.

Stream Protocol

class gruvi.StreamProtocol(timeout=None)

Bases: gruvi.Protocol

Byte stream protocol.

stream

A ReadWriteStream instance that provides blocking, flow controlled read and write access to the underlying transport.

Stream Client and Server

class gruvi.StreamClient(timeout=None)

Bases: gruvi.Client

A stream client.

stream

An alias for self.protocol.stream

read(size=-1)

A alias for self.stream.read().

read1(size=-1)

A alias for self.stream.read1().

readline(limit=-1, delim='n')

A alias for self.stream.readline().

readlines(hint=-1)

A alias for self.stream.readlines().

write(data)

A alias for self.stream.write().

write_eof()

A alias for self.stream.write_eof().

writelines(seq)

A alias for self.stream.writelines().

class gruvi.StreamServer(stream_handler, timeout=None)

Bases: gruvi.Server

A stream server.

The stream_handler argument is a handler function to handle client connections. The handler will be called as stream_handler(stream, transport, protocol). The handler for each connection will run in a separate fiber so it can use blocking I/O on the stream. When the handler returns, the stream is closed.

See Example 2: echo server, using a StreamServer for an example.

Table Of Contents

Previous topic

Transports and protocols (low-level API)

Next topic

Working with Processes