Skip to content
Back to top

# `gevent.selectors` – High-level IO Multiplexing

This module provides `GeventSelector`, a high-level IO multiplexing mechanism. This is aliased to `DefaultSelector`.

This module provides the same API as the selectors defined in `selectors`.

On Python 2, this module is only available if the selectors2 backport is installed.

Added in version 20.6.0.

DefaultSelector

  alias of `GeventSelector`

class GeventSelector(hub=None)

  Bases: `_BaseSelectorImpl`

  A selector implementation using gevent primitives.

  This is a type of `selectors.BaseSelector`, so the documentation for that class applies here.

  Caution

  As the base class indicates, it is critically important to unregister file objects before closing them. (Or close the selector they are registered with before closing them.) Failure to do so may crash the process or have other unintended results.

  close()

    Close the selector.

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

  register(fileobj, events, data=None)

    Register a file object for selection, monitoring it for I/O events.

    fileobj is the file object to monitor. It may either be an integer file descriptor or an object with a `fileno()` method. events is a bitwise mask of events to monitor. data is an opaque object.

    Returns:

      A new `SelectorKey` instance.

    Raises:

      - ValueError – In case of invalid event mask or file descriptor

      - KeyError – if the file object is already registered.

    Changed in version 25.8.1: More reliably raises a `ValueError` if the file descriptor is invalid.

  select(timeout=None)

    Poll for I/O.

    Note that, like the built-in selectors, this will block indefinitely if no timeout is given and no files have been registered.

  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)