Skip to main content

Simple IO channels library

Project description

Channels Build Status codecov BCH compliance donate using paypal

Simple wrapper around file objects and sockets that provides uniform interface to both.

Example:

pipe_chan = PipeChannel(sys.stdin.fileno(), sys.stdout.fileno())
sock_chan = SocketChannel(socket.create_connection(('127.0.0.1', 8080))

Classes

Channel

Channel is the base class for different channels. Every channel implements the following methods:

read(self)

Performs a non-blocking read and returns any bytes available. Raises EndpointClosedException if the channel is closed.

write(self, *data)

Writes chunks of bytes to the channel. Raises EndpointClosedException.

close(self)

Closes the channel and frees up the resources.

get_fd(self)

Returns a file descriptor number that can be used for poll or epoll for reading. Raises NotImplementedError if (custom) channel doesn't support reading.

The following channel classes are implemented:

PipeChannel

PipeChannel(faucet=None, sink=None)

faucet should be a file descriptor open for reading. sink should be a file descriptor open for writing. If both are provided, the channel is bi-directional. Sets faucet to non-blocking mode.

SocketChannel

SocketChannel(sock)

Wraps a socket for non-blocking IO.

LineChannel

LineChannel(channel)

Accepts another channel. read() returns one line at time or b'' if no full line is available. If underlying channel is closed, read() will keep returning lines until everything is returned (last line might not be ending with b'\n').

TestChannel

(in package channels.testing)

Provides put and get methods to to feed data to read and fetch "written" data respectively.

Poller

Poller is a wrapper for select.poll that also supports accepting and keeping track of TCP/Unix clients.

register(self, channel)

Registers the channel for polling.

add_server(self, sock)

Registers a server socket. Poller will accept incoming connections and automatically register clients.

unregister(self, channel)

Removes a registered channel.

close_all(self)

Closes all registered channels and servers.

poll(self, timeout=None)

Performs a single call to select.poll(). timeout is the number of seconds for polling or None for infinite polling. Return value is a list of pairs in format of (data, channel) for channels and ((addr, client_channel), sock) for server sockets. addr depends on socket type.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yet-another-io-channels-library-0.1.1.tar.gz (4.1 kB view hashes)

Uploaded Source

Built Distribution

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page