selectors

Selectors module.

This module allows high-level and efficient I/O multiplexing, built upon the select module primitives.

The following code adapted from trollius.selectors.

Module Contents

Classes

_SelectorMapping(self,selector) Mapping of file objects to selector keys.
BaseSelector() Selector abstract base class.
_BaseSelectorImpl(self) Base selector implementation.
SelectSelector(self) Select-based selector.
PollSelector(self) Poll-based selector.
EpollSelector(self) Epoll-based selector.
DevpollSelector(self) Solaris /dev/poll selector.
KqueueSelector(self) Kqueue-based selector.

Functions

_fileobj_to_fd(fileobj) Return a file descriptor from a file object.
_fileobj_to_fd(fileobj)

Return a file descriptor from a file object.

Parameters: fileobj – file object or file descriptor

Returns: corresponding file descriptor

Raises: ValueError if the object is invalid

class _SelectorMapping(selector)

Mapping of file objects to selector keys.

__init__(selector)
__len__()
__getitem__(fileobj)
__iter__()
class BaseSelector

Selector abstract base class.

A selector supports registering file objects to be monitored for specific I/O events.

A file object is a file descriptor or any object with a fileno() method. An arbitrary object can be attached to the file object, which can be used for example to store context information, a callback, etc.

A selector can use various implementations (select(), poll(), epoll()…) depending on the platform. The default Selector class uses the most efficient implementation on the current platform.

register(fileobj, events, data=None)

Register a file object.

Parameters: fileobj – file object or file descriptor events – events to monitor (bitwise mask of EVENT_READ|EVENT_WRITE) data – attached data

Returns: SelectorKey instance

Raises: ValueError if events is invalid KeyError if fileobj is already registered OSError if fileobj is closed or otherwise is unacceptable to

the underlying system call (if a system call is made)

Note: OSError may or may not be raised

unregister(fileobj)

Unregister a file object.

Parameters: fileobj – file object or file descriptor

Returns: SelectorKey instance

Raises: KeyError if fileobj is not registered

Note: If fileobj is registered but has since been closed this does not raise OSError (even if the wrapped syscall does)

modify(fileobj, events, data=None)

Change a registered file object monitored events or attached data.

Parameters: fileobj – file object or file descriptor events – events to monitor (bitwise mask of EVENT_READ|EVENT_WRITE) data – attached data

Returns: SelectorKey instance

Raises: Anything that unregister() or register() raises

select(timeout=None)

Perform the actual selection, until some monitored file objects are ready or a timeout expires.

Parameters: timeout – if timeout > 0, this specifies the maximum wait time, in

seconds if timeout <= 0, the select() call won’t block, and will report the currently ready file objects if timeout is None, select() will block until a monitored file object becomes ready

Returns: list of (key, events) for ready file objects events is a bitwise mask of EVENT_READ|EVENT_WRITE

close()

Close the selector.

This must be called to make sure that any underlying resource is freed.

get_key(fileobj)

Return the key associated to a registered file object.

Returns: SelectorKey for this file object

get_map()

Return a mapping of file objects to selector keys.

__enter__()
__exit__(*args)
class _BaseSelectorImpl

Base selector implementation.

__init__()
_fileobj_lookup(fileobj)

Return a file descriptor from a file object.

This wraps _fileobj_to_fd() to do an exhaustive search in case the object is invalid but we still have it in our map. This is used by unregister() so we can unregister an object that was previously registered even if it is closed. It is also used by _SelectorMapping.

register(fileobj, events, data=None)
unregister(fileobj)
modify(fileobj, events, data=None)
close()
get_map()
_key_from_fd(fd)

Return the key associated to a given file descriptor.

Parameters: fd – file descriptor

Returns: corresponding key, or None if not found

class SelectSelector

Select-based selector.

__init__()
register(fileobj, events, data=None)
unregister(fileobj)
_select(r, w, _, timeout=None)
select(timeout=None)
class PollSelector

Poll-based selector.

__init__()
register(fileobj, events, data=None)
unregister(fileobj)
select(timeout=None)
class EpollSelector

Epoll-based selector.

__init__()
fileno()
register(fileobj, events, data=None)
unregister(fileobj)
select(timeout=None)
close()
class DevpollSelector

Solaris /dev/poll selector.

__init__()
fileno()
register(fileobj, events, data=None)
unregister(fileobj)
select(timeout=None)
close()
class KqueueSelector

Kqueue-based selector.

__init__()
fileno()
register(fileobj, events, data=None)
unregister(fileobj)
select(timeout=None)
close()