| 
									
										
										
										
											2016-12-04 10:20:55 +02:00
										 |  |  | :mod:`selectors` --- High-level I/O multiplexing
 | 
					
						
							|  |  |  | ================================================
 | 
					
						
							| 
									
										
										
										
											2013-09-04 19:02:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. module:: selectors
 | 
					
						
							|  |  |  |    :synopsis: High-level I/O multiplexing.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. versionadded:: 3.4
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-11 15:02:54 -04:00
										 |  |  | **Source code:** :source:`Lib/selectors.py`
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | --------------
 | 
					
						
							| 
									
										
										
										
											2013-09-04 19:02:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | Introduction
 | 
					
						
							|  |  |  | ------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This module allows high-level and efficient I/O multiplexing, built upon the
 | 
					
						
							|  |  |  | :mod:`select` module primitives. Users are encouraged to use this module
 | 
					
						
							|  |  |  | instead, unless they want precise control over the OS-level primitives used.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | It defines a :class:`BaseSelector` abstract base class, along with several
 | 
					
						
							|  |  |  | concrete implementations (:class:`KqueueSelector`, :class:`EpollSelector`...),
 | 
					
						
							|  |  |  | that can be used to wait for I/O readiness notification on multiple file
 | 
					
						
							|  |  |  | objects. In the following, "file object" refers to any object with a
 | 
					
						
							|  |  |  | :meth:`fileno()` method, or a raw file descriptor. See :term:`file object`.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :class:`DefaultSelector` is an alias to the most efficient implementation
 | 
					
						
							|  |  |  | available on the current platform: this should be the default choice for most
 | 
					
						
							|  |  |  | users.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. note::
 | 
					
						
							|  |  |  |    The type of file objects supported depends on the platform: on Windows,
 | 
					
						
							|  |  |  |    sockets are supported, but not pipes, whereas on Unix, both are supported
 | 
					
						
							|  |  |  |    (some other types may be supported as well, such as fifos or special file
 | 
					
						
							|  |  |  |    devices).
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. seealso::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    :mod:`select`
 | 
					
						
							|  |  |  |       Low-level I/O multiplexing module.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-02 21:00:41 +02:00
										 |  |  | .. include:: ../includes/wasm-notavail.rst
 | 
					
						
							| 
									
										
										
										
											2013-09-04 19:02:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | Classes
 | 
					
						
							|  |  |  | -------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Classes hierarchy::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    BaseSelector
 | 
					
						
							|  |  |  |    +-- SelectSelector
 | 
					
						
							|  |  |  |    +-- PollSelector
 | 
					
						
							|  |  |  |    +-- EpollSelector
 | 
					
						
							| 
									
										
										
										
											2014-03-20 21:43:41 +01:00
										 |  |  |    +-- DevpollSelector
 | 
					
						
							| 
									
										
										
										
											2013-09-04 19:02:49 +02:00
										 |  |  |    +-- KqueueSelector
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | In the following, *events* is a bitwise mask indicating which I/O events should
 | 
					
						
							| 
									
										
										
										
											2015-09-18 15:21:02 -07:00
										 |  |  | be waited for on a given file object. It can be a combination of the modules
 | 
					
						
							|  |  |  | constants below:
 | 
					
						
							| 
									
										
										
										
											2013-09-04 19:02:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |    +-----------------------+-----------------------------------------------+
 | 
					
						
							|  |  |  |    | Constant              | Meaning                                       |
 | 
					
						
							|  |  |  |    +=======================+===============================================+
 | 
					
						
							| 
									
										
										
										
											2023-07-29 08:48:10 +03:00
										 |  |  |    | .. data:: EVENT_READ  | Available for read                            |
 | 
					
						
							| 
									
										
										
										
											2013-09-04 19:02:49 +02:00
										 |  |  |    +-----------------------+-----------------------------------------------+
 | 
					
						
							| 
									
										
										
										
											2023-07-29 08:48:10 +03:00
										 |  |  |    | .. data:: EVENT_WRITE | Available for write                           |
 | 
					
						
							| 
									
										
										
										
											2013-09-04 19:02:49 +02:00
										 |  |  |    +-----------------------+-----------------------------------------------+
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. class:: SelectorKey
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    A :class:`SelectorKey` is a :class:`~collections.namedtuple` used to
 | 
					
						
							| 
									
										
										
										
											2017-05-02 21:27:57 +08:00
										 |  |  |    associate a file object to its underlying file descriptor, selected event
 | 
					
						
							| 
									
										
										
										
											2013-09-04 19:02:49 +02:00
										 |  |  |    mask and attached data. It is returned by several :class:`BaseSelector`
 | 
					
						
							|  |  |  |    methods.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. attribute:: fileobj
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       File object registered.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. attribute:: fd
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       Underlying file descriptor.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. attribute:: events
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-22 16:15:28 -05:00
										 |  |  |       Events that must be waited for on this file object.
 | 
					
						
							| 
									
										
										
										
											2013-09-04 19:02:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |    .. attribute:: data
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       Optional opaque data associated to this file object: for example, this
 | 
					
						
							| 
									
										
										
										
											2013-11-22 16:15:28 -05:00
										 |  |  |       could be used to store a per-client session ID.
 | 
					
						
							| 
									
										
										
										
											2013-09-04 19:02:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. class:: BaseSelector
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    A :class:`BaseSelector` is used to wait for I/O event readiness on multiple
 | 
					
						
							|  |  |  |    file objects. It supports file stream registration, unregistration, and a
 | 
					
						
							|  |  |  |    method to wait for I/O events on those streams, with an optional timeout.
 | 
					
						
							|  |  |  |    It's an abstract base class, so cannot be instantiated. Use
 | 
					
						
							|  |  |  |    :class:`DefaultSelector` instead, or one of :class:`SelectSelector`,
 | 
					
						
							|  |  |  |    :class:`KqueueSelector` etc. if you want to specifically use an
 | 
					
						
							|  |  |  |    implementation, and your platform supports it.
 | 
					
						
							|  |  |  |    :class:`BaseSelector` and its concrete implementations support the
 | 
					
						
							|  |  |  |    :term:`context manager` protocol.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-08 12:14:50 +02:00
										 |  |  |    .. abstractmethod:: register(fileobj, events, data=None)
 | 
					
						
							| 
									
										
										
										
											2013-09-04 19:02:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       Register a file object for selection, monitoring it for I/O events.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-07 15:57:01 -08:00
										 |  |  |       *fileobj* is the file object to monitor.  It may either be an integer
 | 
					
						
							|  |  |  |       file descriptor or an object with a ``fileno()`` method.
 | 
					
						
							| 
									
										
										
										
											2013-09-04 19:02:49 +02:00
										 |  |  |       *events* is a bitwise mask of events to monitor.
 | 
					
						
							|  |  |  |       *data* is an opaque object.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       This returns a new :class:`SelectorKey` instance, or raises a
 | 
					
						
							|  |  |  |       :exc:`ValueError` in case of invalid event mask or file descriptor, or
 | 
					
						
							|  |  |  |       :exc:`KeyError` if the file object is already registered.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-08 12:14:50 +02:00
										 |  |  |    .. abstractmethod:: unregister(fileobj)
 | 
					
						
							| 
									
										
										
										
											2013-09-04 19:02:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       Unregister a file object from selection, removing it from monitoring. A
 | 
					
						
							|  |  |  |       file object shall be unregistered prior to being closed.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       *fileobj* must be a file object previously registered.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       This returns the associated :class:`SelectorKey` instance, or raises a
 | 
					
						
							| 
									
										
										
										
											2013-12-07 15:57:01 -08:00
										 |  |  |       :exc:`KeyError` if *fileobj* is not registered.  It will raise
 | 
					
						
							|  |  |  |       :exc:`ValueError` if *fileobj* is invalid (e.g. it has no ``fileno()``
 | 
					
						
							|  |  |  |       method or its ``fileno()`` method has an invalid return value).
 | 
					
						
							| 
									
										
										
										
											2013-09-04 19:02:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |    .. method:: modify(fileobj, events, data=None)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-22 16:15:28 -05:00
										 |  |  |       Change a registered file object's monitored events or attached data.
 | 
					
						
							| 
									
										
										
										
											2013-09-04 19:02:49 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-29 08:48:10 +03:00
										 |  |  |       This is equivalent to ``BaseSelector.unregister(fileobj)`` followed
 | 
					
						
							|  |  |  |       by ``BaseSelector.register(fileobj, events, data)``, except that it
 | 
					
						
							| 
									
										
										
										
											2013-09-04 19:02:49 +02:00
										 |  |  |       can be implemented more efficiently.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       This returns a new :class:`SelectorKey` instance, or raises a
 | 
					
						
							|  |  |  |       :exc:`ValueError` in case of invalid event mask or file descriptor, or
 | 
					
						
							|  |  |  |       :exc:`KeyError` if the file object is not registered.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-08 12:14:50 +02:00
										 |  |  |    .. abstractmethod:: select(timeout=None)
 | 
					
						
							| 
									
										
										
										
											2013-09-04 19:02:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       Wait until some registered file objects become ready, or the timeout
 | 
					
						
							|  |  |  |       expires.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       If ``timeout > 0``, this specifies the maximum wait time, in seconds.
 | 
					
						
							|  |  |  |       If ``timeout <= 0``, the call won't block, and will report the currently
 | 
					
						
							|  |  |  |       ready file objects.
 | 
					
						
							|  |  |  |       If *timeout* is ``None``, the call will block until a monitored file object
 | 
					
						
							|  |  |  |       becomes ready.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-22 16:15:28 -05:00
										 |  |  |       This returns a list of ``(key, events)`` tuples, one for each ready file
 | 
					
						
							| 
									
										
										
										
											2013-09-04 19:02:49 +02:00
										 |  |  |       object.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       *key* is the :class:`SelectorKey` instance corresponding to a ready file
 | 
					
						
							|  |  |  |       object.
 | 
					
						
							|  |  |  |       *events* is a bitmask of events ready on this file object.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-01 13:23:48 +01:00
										 |  |  |       .. note::
 | 
					
						
							|  |  |  |           This method can return before any file object becomes ready or the
 | 
					
						
							|  |  |  |           timeout has elapsed if the current process receives a signal: in this
 | 
					
						
							|  |  |  |           case, an empty list will be returned.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-31 12:08:09 +02:00
										 |  |  |       .. versionchanged:: 3.5
 | 
					
						
							|  |  |  |          The selector is now retried with a recomputed timeout when interrupted
 | 
					
						
							|  |  |  |          by a signal if the signal handler did not raise an exception (see
 | 
					
						
							|  |  |  |          :pep:`475` for the rationale), instead of returning an empty list
 | 
					
						
							|  |  |  |          of events before the timeout.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-04 19:02:49 +02:00
										 |  |  |    .. method:: close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       Close the selector.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       This must be called to make sure that any underlying resource is freed.
 | 
					
						
							|  |  |  |       The selector shall not be used once it has been closed.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. method:: get_key(fileobj)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-22 16:15:28 -05:00
										 |  |  |       Return the key associated with a registered file object.
 | 
					
						
							| 
									
										
										
										
											2013-09-04 19:02:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       This returns the :class:`SelectorKey` instance associated to this file
 | 
					
						
							|  |  |  |       object, or raises :exc:`KeyError` if the file object is not registered.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-08 12:14:50 +02:00
										 |  |  |    .. abstractmethod:: get_map()
 | 
					
						
							| 
									
										
										
										
											2013-10-30 20:31:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       Return a mapping of file objects to selector keys.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       This returns a :class:`~collections.abc.Mapping` instance mapping
 | 
					
						
							|  |  |  |       registered file objects to their associated :class:`SelectorKey`
 | 
					
						
							|  |  |  |       instance.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-04 19:02:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. class:: DefaultSelector()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The default selector class, using the most efficient implementation
 | 
					
						
							|  |  |  |    available on the current platform. This should be the default choice for
 | 
					
						
							|  |  |  |    most users.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. class:: SelectSelector()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    :func:`select.select`-based selector.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. class:: PollSelector()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    :func:`select.poll`-based selector.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. class:: EpollSelector()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    :func:`select.epoll`-based selector.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. method:: fileno()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       This returns the file descriptor used by the underlying
 | 
					
						
							|  |  |  |       :func:`select.epoll` object.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-20 21:43:41 +01:00
										 |  |  | .. class:: DevpollSelector()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    :func:`select.devpoll`-based selector.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. method:: fileno()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       This returns the file descriptor used by the underlying
 | 
					
						
							|  |  |  |       :func:`select.devpoll` object.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-24 09:06:33 +01:00
										 |  |  |    .. versionadded:: 3.5
 | 
					
						
							| 
									
										
										
										
											2013-09-04 19:02:49 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | .. class:: KqueueSelector()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    :func:`select.kqueue`-based selector.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    .. method:: fileno()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       This returns the file descriptor used by the underlying
 | 
					
						
							|  |  |  |       :func:`select.kqueue` object.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-29 14:51:18 -08:00
										 |  |  | Examples
 | 
					
						
							|  |  |  | --------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Here is a simple echo server implementation::
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    import selectors
 | 
					
						
							|  |  |  |    import socket
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    sel = selectors.DefaultSelector()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    def accept(sock, mask):
 | 
					
						
							|  |  |  |        conn, addr = sock.accept()  # Should be ready
 | 
					
						
							|  |  |  |        print('accepted', conn, 'from', addr)
 | 
					
						
							|  |  |  |        conn.setblocking(False)
 | 
					
						
							|  |  |  |        sel.register(conn, selectors.EVENT_READ, read)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    def read(conn, mask):
 | 
					
						
							|  |  |  |        data = conn.recv(1000)  # Should be ready
 | 
					
						
							|  |  |  |        if data:
 | 
					
						
							|  |  |  |            print('echoing', repr(data), 'to', conn)
 | 
					
						
							|  |  |  |            conn.send(data)  # Hope it won't block
 | 
					
						
							|  |  |  |        else:
 | 
					
						
							|  |  |  |            print('closing', conn)
 | 
					
						
							|  |  |  |            sel.unregister(conn)
 | 
					
						
							|  |  |  |            conn.close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    sock = socket.socket()
 | 
					
						
							|  |  |  |    sock.bind(('localhost', 1234))
 | 
					
						
							|  |  |  |    sock.listen(100)
 | 
					
						
							|  |  |  |    sock.setblocking(False)
 | 
					
						
							|  |  |  |    sel.register(sock, selectors.EVENT_READ, accept)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    while True:
 | 
					
						
							|  |  |  |        events = sel.select()
 | 
					
						
							|  |  |  |        for key, mask in events:
 | 
					
						
							|  |  |  |            callback = key.data
 | 
					
						
							|  |  |  |            callback(key.fileobj, mask)
 |