Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`select` --- Waiting for I/O completion |
| 2 | ============================================ |
| 3 | |
| 4 | .. module:: select |
| 5 | :synopsis: Wait for I/O completion on multiple streams. |
| 6 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 7 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 8 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 9 | This module provides access to the :c:func:`select` and :c:func:`poll` functions |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 10 | available in most operating systems, :c:func:`devpoll` available on |
| 11 | Solaris and derivatives, :c:func:`epoll` available on Linux 2.5+ and |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 12 | :c:func:`kqueue` available on most BSD. |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 13 | Note that on Windows, it only works for sockets; on other operating systems, |
| 14 | it also works for other file types (in particular, on Unix, it works on pipes). |
| 15 | It cannot be used on regular files to determine whether a file has grown since |
| 16 | it was last read. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 | |
Victor Stinner | 73821c4 | 2013-09-04 20:40:13 +0200 | [diff] [blame] | 18 | .. note:: |
| 19 | |
| 20 | The :mod:`selectors` module allows high-level and efficient I/O |
| 21 | multiplexing, built upon the :mod:`select` module primitives. Users are |
| 22 | encouraged to use the :mod:`selectors` module instead, unless they want |
| 23 | precise control over the OS-level primitives used. |
| 24 | |
| 25 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 26 | The module defines the following: |
| 27 | |
| 28 | |
| 29 | .. exception:: error |
| 30 | |
Antoine Pitrou | 9b7fcf8 | 2011-10-12 16:23:02 +0200 | [diff] [blame] | 31 | A deprecated alias of :exc:`OSError`. |
| 32 | |
| 33 | .. versionchanged:: 3.3 |
| 34 | Following :pep:`3151`, this class was made an alias of :exc:`OSError`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 35 | |
| 36 | |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 37 | .. function:: devpoll() |
Jesus Cea | f450c1b | 2011-11-15 05:42:59 +0100 | [diff] [blame] | 38 | |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 39 | (Only supported on Solaris and derivatives.) Returns a ``/dev/poll`` |
| 40 | polling object; see section :ref:`devpoll-objects` below for the |
| 41 | methods supported by devpoll objects. |
| 42 | |
| 43 | :c:func:`devpoll` objects are linked to the number of file |
| 44 | descriptors allowed at the time of instantiation. If your program |
| 45 | reduces this value, :c:func:`devpoll` will fail. If your program |
Jesus Cea | f450c1b | 2011-11-15 05:42:59 +0100 | [diff] [blame] | 46 | increases this value, :c:func:`devpoll` may return an |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 47 | incomplete list of active file descriptors. |
| 48 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 49 | The new file descriptor is :ref:`non-inheritable <fd_inheritance>`. |
| 50 | |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 51 | .. versionadded:: 3.3 |
| 52 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 53 | .. versionchanged:: 3.4 |
| 54 | The new file descriptor is now non-inheritable. |
| 55 | |
Benjamin Peterson | 2fb9ae9 | 2011-12-27 15:15:41 -0600 | [diff] [blame] | 56 | .. function:: epoll(sizehint=-1, flags=0) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 57 | |
Benjamin Peterson | 2fb9ae9 | 2011-12-27 15:15:41 -0600 | [diff] [blame] | 58 | (Only supported on Linux 2.5.44 and newer.) Return an edge polling object, |
| 59 | which can be used as Edge or Level Triggered interface for I/O |
| 60 | events. *sizehint* is deprecated and completely ignored. *flags* can be set |
| 61 | to :const:`EPOLL_CLOEXEC`, which causes the epoll descriptor to be closed |
R David Murray | 2bc930f | 2013-12-31 11:17:21 -0500 | [diff] [blame] | 62 | automatically when :func:`os.execve` is called. |
| 63 | |
| 64 | See the :ref:`epoll-objects` section below for the methods supported by |
| 65 | epolling objects. |
| 66 | |
| 67 | ``epoll`` objects support the context management protocol: when used in a |
| 68 | :keyword:`with` statement, the new file descriptor is automatically closed |
| 69 | at the end of the block. |
Benjamin Peterson | 2fb9ae9 | 2011-12-27 15:15:41 -0600 | [diff] [blame] | 70 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 71 | The new file descriptor is :ref:`non-inheritable <fd_inheritance>`. |
| 72 | |
Benjamin Peterson | 2fb9ae9 | 2011-12-27 15:15:41 -0600 | [diff] [blame] | 73 | .. versionchanged:: 3.3 |
Benjamin Peterson | 2fb9ae9 | 2011-12-27 15:15:41 -0600 | [diff] [blame] | 74 | Added the *flags* parameter. |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 75 | |
Antoine Pitrou | 09bb89b | 2012-12-15 21:14:21 +0100 | [diff] [blame] | 76 | .. versionchanged:: 3.4 |
| 77 | Support for the :keyword:`with` statement was added. |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 78 | The new file descriptor is now non-inheritable. |
Antoine Pitrou | 09bb89b | 2012-12-15 21:14:21 +0100 | [diff] [blame] | 79 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 80 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 81 | .. function:: poll() |
| 82 | |
| 83 | (Not supported by all operating systems.) Returns a polling object, which |
| 84 | supports registering and unregistering file descriptors, and then polling them |
| 85 | for I/O events; see section :ref:`poll-objects` below for the methods supported |
| 86 | by polling objects. |
| 87 | |
| 88 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 89 | .. function:: kqueue() |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 90 | |
Georg Brandl | e767e04 | 2010-07-14 08:00:22 +0000 | [diff] [blame] | 91 | (Only supported on BSD.) Returns a kernel queue object; see section |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 92 | :ref:`kqueue-objects` below for the methods supported by kqueue objects. |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 93 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 94 | The new file descriptor is :ref:`non-inheritable <fd_inheritance>`. |
| 95 | |
| 96 | .. versionchanged:: 3.4 |
| 97 | The new file descriptor is now non-inheritable. |
| 98 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 99 | |
Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 100 | .. function:: kevent(ident, filter=KQ_FILTER_READ, flags=KQ_EV_ADD, fflags=0, data=0, udata=0) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 101 | |
Georg Brandl | e767e04 | 2010-07-14 08:00:22 +0000 | [diff] [blame] | 102 | (Only supported on BSD.) Returns a kernel event object; see section |
| 103 | :ref:`kevent-objects` below for the methods supported by kevent objects. |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 104 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 105 | |
Georg Brandl | 734e268 | 2008-08-12 08:18:18 +0000 | [diff] [blame] | 106 | .. function:: select(rlist, wlist, xlist[, timeout]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 107 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 108 | This is a straightforward interface to the Unix :c:func:`select` system call. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 109 | The first three arguments are sequences of 'waitable objects': either |
| 110 | integers representing file descriptors or objects with a parameterless method |
Serhiy Storchaka | 9e0ae53 | 2013-08-24 00:23:38 +0300 | [diff] [blame] | 111 | named :meth:`~io.IOBase.fileno` returning such an integer: |
Georg Brandl | 734e268 | 2008-08-12 08:18:18 +0000 | [diff] [blame] | 112 | |
| 113 | * *rlist*: wait until ready for reading |
| 114 | * *wlist*: wait until ready for writing |
| 115 | * *xlist*: wait for an "exceptional condition" (see the manual page for what |
| 116 | your system considers such a condition) |
| 117 | |
| 118 | Empty sequences are allowed, but acceptance of three empty sequences is |
| 119 | platform-dependent. (It is known to work on Unix but not on Windows.) The |
| 120 | optional *timeout* argument specifies a time-out as a floating point number |
| 121 | in seconds. When the *timeout* argument is omitted the function blocks until |
| 122 | at least one file descriptor is ready. A time-out value of zero specifies a |
| 123 | poll and never blocks. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 124 | |
| 125 | The return value is a triple of lists of objects that are ready: subsets of the |
| 126 | first three arguments. When the time-out is reached without a file descriptor |
| 127 | becoming ready, three empty lists are returned. |
| 128 | |
| 129 | .. index:: |
| 130 | single: socket() (in module socket) |
| 131 | single: popen() (in module os) |
| 132 | |
Antoine Pitrou | 11cb961 | 2010-09-15 11:11:28 +0000 | [diff] [blame] | 133 | Among the acceptable object types in the sequences are Python :term:`file |
| 134 | objects <file object>` (e.g. ``sys.stdin``, or objects returned by |
| 135 | :func:`open` or :func:`os.popen`), socket objects returned by |
| 136 | :func:`socket.socket`. You may also define a :dfn:`wrapper` class yourself, |
Serhiy Storchaka | 9e0ae53 | 2013-08-24 00:23:38 +0300 | [diff] [blame] | 137 | as long as it has an appropriate :meth:`~io.IOBase.fileno` method (that |
| 138 | really returns a file descriptor, not just a random integer). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 139 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 140 | .. note:: |
| 141 | |
| 142 | .. index:: single: WinSock |
| 143 | |
Georg Brandl | 734e268 | 2008-08-12 08:18:18 +0000 | [diff] [blame] | 144 | File objects on Windows are not acceptable, but sockets are. On Windows, |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 145 | the underlying :c:func:`select` function is provided by the WinSock |
Georg Brandl | 734e268 | 2008-08-12 08:18:18 +0000 | [diff] [blame] | 146 | library, and does not handle file descriptors that don't originate from |
| 147 | WinSock. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 148 | |
Victor Stinner | f70e1ca | 2015-03-30 21:16:11 +0200 | [diff] [blame] | 149 | .. versionchanged:: 3.5 |
| 150 | The function is now retried with a recomputed timeout when interrupted by |
| 151 | a signal, except if the signal handler raises an exception (see |
| 152 | :pep:`475` for the rationale), instead of raising |
| 153 | :exc:`InterruptedError`. |
| 154 | |
| 155 | |
Antoine Pitrou | cfad97b | 2011-01-25 17:24:57 +0000 | [diff] [blame] | 156 | .. attribute:: PIPE_BUF |
Gregory P. Smith | b970b86 | 2009-07-04 02:28:47 +0000 | [diff] [blame] | 157 | |
Antoine Pitrou | da7be3e | 2011-01-25 16:28:44 +0000 | [diff] [blame] | 158 | The minimum number of bytes which can be written without blocking to a pipe |
Serhiy Storchaka | 9e0ae53 | 2013-08-24 00:23:38 +0300 | [diff] [blame] | 159 | when the pipe has been reported as ready for writing by :func:`~select.select`, |
Antoine Pitrou | da7be3e | 2011-01-25 16:28:44 +0000 | [diff] [blame] | 160 | :func:`poll` or another interface in this module. This doesn't apply |
| 161 | to other kind of file-like objects such as sockets. |
| 162 | |
Amaury Forgeot d'Arc | ace3102 | 2009-07-09 22:44:11 +0000 | [diff] [blame] | 163 | This value is guaranteed by POSIX to be at least 512. Availability: Unix. |
Gregory P. Smith | b970b86 | 2009-07-04 02:28:47 +0000 | [diff] [blame] | 164 | |
Mark Dickinson | 574b1d6 | 2009-10-01 20:20:09 +0000 | [diff] [blame] | 165 | .. versionadded:: 3.2 |
Gregory P. Smith | b970b86 | 2009-07-04 02:28:47 +0000 | [diff] [blame] | 166 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 167 | |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 168 | .. _devpoll-objects: |
| 169 | |
| 170 | ``/dev/poll`` Polling Objects |
Georg Brandl | 525d355 | 2014-10-29 10:26:56 +0100 | [diff] [blame] | 171 | ----------------------------- |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 172 | |
| 173 | Solaris and derivatives have ``/dev/poll``. While :c:func:`select` is |
| 174 | O(highest file descriptor) and :c:func:`poll` is O(number of file |
| 175 | descriptors), ``/dev/poll`` is O(active file descriptors). |
| 176 | |
| 177 | ``/dev/poll`` behaviour is very close to the standard :c:func:`poll` |
| 178 | object. |
| 179 | |
| 180 | |
Victor Stinner | 13423c3 | 2013-08-22 00:19:50 +0200 | [diff] [blame] | 181 | .. method:: devpoll.close() |
| 182 | |
| 183 | Close the file descriptor of the polling object. |
| 184 | |
| 185 | .. versionadded:: 3.4 |
| 186 | |
| 187 | |
| 188 | .. attribute:: devpoll.closed |
| 189 | |
| 190 | ``True`` if the polling object is closed. |
| 191 | |
| 192 | .. versionadded:: 3.4 |
| 193 | |
| 194 | |
| 195 | .. method:: devpoll.fileno() |
| 196 | |
| 197 | Return the file descriptor number of the polling object. |
| 198 | |
| 199 | .. versionadded:: 3.4 |
| 200 | |
| 201 | |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 202 | .. method:: devpoll.register(fd[, eventmask]) |
| 203 | |
| 204 | Register a file descriptor with the polling object. Future calls to the |
Serhiy Storchaka | 9e0ae53 | 2013-08-24 00:23:38 +0300 | [diff] [blame] | 205 | :meth:`poll` method will then check whether the file descriptor has any |
| 206 | pending I/O events. *fd* can be either an integer, or an object with a |
| 207 | :meth:`~io.IOBase.fileno` method that returns an integer. File objects |
| 208 | implement :meth:`!fileno`, so they can also be used as the argument. |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 209 | |
| 210 | *eventmask* is an optional bitmask describing the type of events you want to |
| 211 | check for. The constants are the same that with :c:func:`poll` |
| 212 | object. The default value is a combination of the constants :const:`POLLIN`, |
| 213 | :const:`POLLPRI`, and :const:`POLLOUT`. |
| 214 | |
| 215 | .. warning:: |
| 216 | |
| 217 | Registering a file descriptor that's already registered is not an |
Donald Stufft | 8b852f1 | 2014-05-20 12:58:38 -0400 | [diff] [blame] | 218 | error, but the result is undefined. The appropriate action is to |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 219 | unregister or modify it first. This is an important difference |
| 220 | compared with :c:func:`poll`. |
| 221 | |
| 222 | |
| 223 | .. method:: devpoll.modify(fd[, eventmask]) |
| 224 | |
| 225 | This method does an :meth:`unregister` followed by a |
| 226 | :meth:`register`. It is (a bit) more efficient that doing the same |
| 227 | explicitly. |
| 228 | |
| 229 | |
| 230 | .. method:: devpoll.unregister(fd) |
| 231 | |
| 232 | Remove a file descriptor being tracked by a polling object. Just like the |
| 233 | :meth:`register` method, *fd* can be an integer or an object with a |
Serhiy Storchaka | 9e0ae53 | 2013-08-24 00:23:38 +0300 | [diff] [blame] | 234 | :meth:`~io.IOBase.fileno` method that returns an integer. |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 235 | |
| 236 | Attempting to remove a file descriptor that was never registered is |
| 237 | safely ignored. |
| 238 | |
| 239 | |
| 240 | .. method:: devpoll.poll([timeout]) |
| 241 | |
| 242 | Polls the set of registered file descriptors, and returns a possibly-empty list |
| 243 | containing ``(fd, event)`` 2-tuples for the descriptors that have events or |
| 244 | errors to report. *fd* is the file descriptor, and *event* is a bitmask with |
| 245 | bits set for the reported events for that descriptor --- :const:`POLLIN` for |
| 246 | waiting input, :const:`POLLOUT` to indicate that the descriptor can be written |
| 247 | to, and so forth. An empty list indicates that the call timed out and no file |
| 248 | descriptors had any events to report. If *timeout* is given, it specifies the |
| 249 | length of time in milliseconds which the system will wait for events before |
| 250 | returning. If *timeout* is omitted, -1, or :const:`None`, the call will |
| 251 | block until there is an event for this poll object. |
| 252 | |
Victor Stinner | 45ca48b | 2015-03-31 12:10:33 +0200 | [diff] [blame] | 253 | .. versionchanged:: 3.5 |
| 254 | The function is now retried with a recomputed timeout when interrupted by |
| 255 | a signal, except if the signal handler raises an exception (see |
| 256 | :pep:`475` for the rationale), instead of raising |
| 257 | :exc:`InterruptedError`. |
| 258 | |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 259 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 260 | .. _epoll-objects: |
| 261 | |
| 262 | Edge and Level Trigger Polling (epoll) Objects |
| 263 | ---------------------------------------------- |
| 264 | |
| 265 | http://linux.die.net/man/4/epoll |
| 266 | |
| 267 | *eventmask* |
| 268 | |
| 269 | +-----------------------+-----------------------------------------------+ |
| 270 | | Constant | Meaning | |
| 271 | +=======================+===============================================+ |
| 272 | | :const:`EPOLLIN` | Available for read | |
| 273 | +-----------------------+-----------------------------------------------+ |
| 274 | | :const:`EPOLLOUT` | Available for write | |
| 275 | +-----------------------+-----------------------------------------------+ |
| 276 | | :const:`EPOLLPRI` | Urgent data for read | |
| 277 | +-----------------------+-----------------------------------------------+ |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 278 | | :const:`EPOLLERR` | Error condition happened on the assoc. fd | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 279 | +-----------------------+-----------------------------------------------+ |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 280 | | :const:`EPOLLHUP` | Hang up happened on the assoc. fd | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 281 | +-----------------------+-----------------------------------------------+ |
| 282 | | :const:`EPOLLET` | Set Edge Trigger behavior, the default is | |
| 283 | | | Level Trigger behavior | |
| 284 | +-----------------------+-----------------------------------------------+ |
| 285 | | :const:`EPOLLONESHOT` | Set one-shot behavior. After one event is | |
| 286 | | | pulled out, the fd is internally disabled | |
| 287 | +-----------------------+-----------------------------------------------+ |
Jean-Paul Calderone | 7f54dce | 2010-07-18 16:30:31 +0000 | [diff] [blame] | 288 | | :const:`EPOLLRDNORM` | Equivalent to :const:`EPOLLIN` | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 289 | +-----------------------+-----------------------------------------------+ |
Jean-Paul Calderone | 7f54dce | 2010-07-18 16:30:31 +0000 | [diff] [blame] | 290 | | :const:`EPOLLRDBAND` | Priority data band can be read. | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 291 | +-----------------------+-----------------------------------------------+ |
Jean-Paul Calderone | 7f54dce | 2010-07-18 16:30:31 +0000 | [diff] [blame] | 292 | | :const:`EPOLLWRNORM` | Equivalent to :const:`EPOLLOUT` | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 293 | +-----------------------+-----------------------------------------------+ |
Jean-Paul Calderone | 7f54dce | 2010-07-18 16:30:31 +0000 | [diff] [blame] | 294 | | :const:`EPOLLWRBAND` | Priority data may be written. | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 295 | +-----------------------+-----------------------------------------------+ |
Jean-Paul Calderone | 7f54dce | 2010-07-18 16:30:31 +0000 | [diff] [blame] | 296 | | :const:`EPOLLMSG` | Ignored. | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 297 | +-----------------------+-----------------------------------------------+ |
| 298 | |
| 299 | |
| 300 | .. method:: epoll.close() |
| 301 | |
| 302 | Close the control file descriptor of the epoll object. |
| 303 | |
| 304 | |
Victor Stinner | 13423c3 | 2013-08-22 00:19:50 +0200 | [diff] [blame] | 305 | .. attribute:: epoll.closed |
| 306 | |
| 307 | ``True`` if the epoll object is closed. |
| 308 | |
| 309 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 310 | .. method:: epoll.fileno() |
| 311 | |
| 312 | Return the file descriptor number of the control fd. |
| 313 | |
| 314 | |
| 315 | .. method:: epoll.fromfd(fd) |
| 316 | |
| 317 | Create an epoll object from a given file descriptor. |
| 318 | |
| 319 | |
| 320 | .. method:: epoll.register(fd[, eventmask]) |
| 321 | |
| 322 | Register a fd descriptor with the epoll object. |
| 323 | |
| 324 | |
| 325 | .. method:: epoll.modify(fd, eventmask) |
| 326 | |
Georg Brandl | 632c812 | 2014-01-12 18:03:12 +0100 | [diff] [blame] | 327 | Modify a registered file descriptor. |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 328 | |
| 329 | |
| 330 | .. method:: epoll.unregister(fd) |
| 331 | |
| 332 | Remove a registered file descriptor from the epoll object. |
| 333 | |
| 334 | |
Hynek Schlawack | dfa4652 | 2012-05-21 11:01:54 +0200 | [diff] [blame] | 335 | .. method:: epoll.poll(timeout=-1, maxevents=-1) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 336 | |
| 337 | Wait for events. timeout in seconds (float) |
| 338 | |
Victor Stinner | 41eba22 | 2015-03-30 21:59:21 +0200 | [diff] [blame] | 339 | .. versionchanged:: 3.5 |
| 340 | The function is now retried with a recomputed timeout when interrupted by |
| 341 | a signal, except if the signal handler raises an exception (see |
| 342 | :pep:`475` for the rationale), instead of raising |
| 343 | :exc:`InterruptedError`. |
| 344 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 345 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 346 | .. _poll-objects: |
| 347 | |
| 348 | Polling Objects |
| 349 | --------------- |
| 350 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 351 | The :c:func:`poll` system call, supported on most Unix systems, provides better |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 352 | scalability for network servers that service many, many clients at the same |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 353 | time. :c:func:`poll` scales better because the system call only requires listing |
| 354 | the file descriptors of interest, while :c:func:`select` builds a bitmap, turns |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 355 | on bits for the fds of interest, and then afterward the whole bitmap has to be |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 356 | linearly scanned again. :c:func:`select` is O(highest file descriptor), while |
| 357 | :c:func:`poll` is O(number of file descriptors). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 358 | |
| 359 | |
| 360 | .. method:: poll.register(fd[, eventmask]) |
| 361 | |
| 362 | Register a file descriptor with the polling object. Future calls to the |
Serhiy Storchaka | 9e0ae53 | 2013-08-24 00:23:38 +0300 | [diff] [blame] | 363 | :meth:`poll` method will then check whether the file descriptor has any |
| 364 | pending I/O events. *fd* can be either an integer, or an object with a |
| 365 | :meth:`~io.IOBase.fileno` method that returns an integer. File objects |
| 366 | implement :meth:`!fileno`, so they can also be used as the argument. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 367 | |
| 368 | *eventmask* is an optional bitmask describing the type of events you want to |
| 369 | check for, and can be a combination of the constants :const:`POLLIN`, |
| 370 | :const:`POLLPRI`, and :const:`POLLOUT`, described in the table below. If not |
| 371 | specified, the default value used will check for all 3 types of events. |
| 372 | |
| 373 | +-------------------+------------------------------------------+ |
| 374 | | Constant | Meaning | |
| 375 | +===================+==========================================+ |
| 376 | | :const:`POLLIN` | There is data to read | |
| 377 | +-------------------+------------------------------------------+ |
| 378 | | :const:`POLLPRI` | There is urgent data to read | |
| 379 | +-------------------+------------------------------------------+ |
| 380 | | :const:`POLLOUT` | Ready for output: writing will not block | |
| 381 | +-------------------+------------------------------------------+ |
| 382 | | :const:`POLLERR` | Error condition of some sort | |
| 383 | +-------------------+------------------------------------------+ |
| 384 | | :const:`POLLHUP` | Hung up | |
| 385 | +-------------------+------------------------------------------+ |
| 386 | | :const:`POLLNVAL` | Invalid request: descriptor not open | |
| 387 | +-------------------+------------------------------------------+ |
| 388 | |
| 389 | Registering a file descriptor that's already registered is not an error, and has |
| 390 | the same effect as registering the descriptor exactly once. |
| 391 | |
| 392 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 393 | .. method:: poll.modify(fd, eventmask) |
| 394 | |
| 395 | Modifies an already registered fd. This has the same effect as |
Jean-Paul Calderone | 7f94f39 | 2010-07-18 16:13:27 +0000 | [diff] [blame] | 396 | ``register(fd, eventmask)``. Attempting to modify a file descriptor |
Andrew Svetlov | 050f9ea | 2014-04-01 00:23:23 +0300 | [diff] [blame] | 397 | that was never registered causes an :exc:`OSError` exception with errno |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 398 | :const:`ENOENT` to be raised. |
| 399 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 400 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 401 | .. method:: poll.unregister(fd) |
| 402 | |
| 403 | Remove a file descriptor being tracked by a polling object. Just like the |
| 404 | :meth:`register` method, *fd* can be an integer or an object with a |
Serhiy Storchaka | 9e0ae53 | 2013-08-24 00:23:38 +0300 | [diff] [blame] | 405 | :meth:`~io.IOBase.fileno` method that returns an integer. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 406 | |
| 407 | Attempting to remove a file descriptor that was never registered causes a |
| 408 | :exc:`KeyError` exception to be raised. |
| 409 | |
| 410 | |
| 411 | .. method:: poll.poll([timeout]) |
| 412 | |
| 413 | Polls the set of registered file descriptors, and returns a possibly-empty list |
| 414 | containing ``(fd, event)`` 2-tuples for the descriptors that have events or |
| 415 | errors to report. *fd* is the file descriptor, and *event* is a bitmask with |
| 416 | bits set for the reported events for that descriptor --- :const:`POLLIN` for |
| 417 | waiting input, :const:`POLLOUT` to indicate that the descriptor can be written |
| 418 | to, and so forth. An empty list indicates that the call timed out and no file |
| 419 | descriptors had any events to report. If *timeout* is given, it specifies the |
| 420 | length of time in milliseconds which the system will wait for events before |
| 421 | returning. If *timeout* is omitted, negative, or :const:`None`, the call will |
| 422 | block until there is an event for this poll object. |
| 423 | |
Victor Stinner | 3c7d6e0 | 2015-03-30 21:38:00 +0200 | [diff] [blame] | 424 | .. versionchanged:: 3.5 |
| 425 | The function is now retried with a recomputed timeout when interrupted by |
| 426 | a signal, except if the signal handler raises an exception (see |
| 427 | :pep:`475` for the rationale), instead of raising |
| 428 | :exc:`InterruptedError`. |
| 429 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 430 | |
| 431 | .. _kqueue-objects: |
| 432 | |
| 433 | Kqueue Objects |
| 434 | -------------- |
| 435 | |
| 436 | .. method:: kqueue.close() |
| 437 | |
| 438 | Close the control file descriptor of the kqueue object. |
| 439 | |
| 440 | |
Victor Stinner | 13423c3 | 2013-08-22 00:19:50 +0200 | [diff] [blame] | 441 | .. attribute:: kqueue.closed |
| 442 | |
| 443 | ``True`` if the kqueue object is closed. |
| 444 | |
| 445 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 446 | .. method:: kqueue.fileno() |
| 447 | |
| 448 | Return the file descriptor number of the control fd. |
| 449 | |
| 450 | |
Benjamin Peterson | 9bc9351 | 2008-09-22 22:10:59 +0000 | [diff] [blame] | 451 | .. method:: kqueue.fromfd(fd) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 452 | |
| 453 | Create a kqueue object from a given file descriptor. |
| 454 | |
| 455 | |
Hynek Schlawack | 979f37a | 2012-05-22 16:12:18 +0200 | [diff] [blame] | 456 | .. method:: kqueue.control(changelist, max_events[, timeout=None]) -> eventlist |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 457 | |
| 458 | Low level interface to kevent |
| 459 | |
| 460 | - changelist must be an iterable of kevent object or None |
| 461 | - max_events must be 0 or a positive integer |
| 462 | - timeout in seconds (floats possible) |
| 463 | |
Victor Stinner | 4448c08 | 2015-03-31 11:48:34 +0200 | [diff] [blame] | 464 | .. versionchanged:: 3.5 |
| 465 | The function is now retried with a recomputed timeout when interrupted by |
| 466 | a signal, except if the signal handler raises an exception (see |
| 467 | :pep:`475` for the rationale), instead of raising |
| 468 | :exc:`InterruptedError`. |
| 469 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 470 | |
| 471 | .. _kevent-objects: |
| 472 | |
| 473 | Kevent Objects |
| 474 | -------------- |
| 475 | |
Serhiy Storchaka | 6dff020 | 2016-05-07 10:49:07 +0300 | [diff] [blame] | 476 | https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2 |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 477 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 478 | .. attribute:: kevent.ident |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 479 | |
| 480 | Value used to identify the event. The interpretation depends on the filter |
| 481 | but it's usually the file descriptor. In the constructor ident can either |
Serhiy Storchaka | 9e0ae53 | 2013-08-24 00:23:38 +0300 | [diff] [blame] | 482 | be an int or an object with a :meth:`~io.IOBase.fileno` method. kevent |
| 483 | stores the integer internally. |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 484 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 485 | .. attribute:: kevent.filter |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 486 | |
Georg Brandl | 1b5ab45 | 2009-08-13 07:56:35 +0000 | [diff] [blame] | 487 | Name of the kernel filter. |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 488 | |
| 489 | +---------------------------+---------------------------------------------+ |
| 490 | | Constant | Meaning | |
| 491 | +===========================+=============================================+ |
| 492 | | :const:`KQ_FILTER_READ` | Takes a descriptor and returns whenever | |
| 493 | | | there is data available to read | |
| 494 | +---------------------------+---------------------------------------------+ |
| 495 | | :const:`KQ_FILTER_WRITE` | Takes a descriptor and returns whenever | |
Georg Brandl | 1b5ab45 | 2009-08-13 07:56:35 +0000 | [diff] [blame] | 496 | | | there is data available to write | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 497 | +---------------------------+---------------------------------------------+ |
| 498 | | :const:`KQ_FILTER_AIO` | AIO requests | |
| 499 | +---------------------------+---------------------------------------------+ |
| 500 | | :const:`KQ_FILTER_VNODE` | Returns when one or more of the requested | |
| 501 | | | events watched in *fflag* occurs | |
| 502 | +---------------------------+---------------------------------------------+ |
| 503 | | :const:`KQ_FILTER_PROC` | Watch for events on a process id | |
| 504 | +---------------------------+---------------------------------------------+ |
| 505 | | :const:`KQ_FILTER_NETDEV` | Watch for events on a network device | |
| 506 | | | [not available on Mac OS X] | |
| 507 | +---------------------------+---------------------------------------------+ |
| 508 | | :const:`KQ_FILTER_SIGNAL` | Returns whenever the watched signal is | |
| 509 | | | delivered to the process | |
| 510 | +---------------------------+---------------------------------------------+ |
| 511 | | :const:`KQ_FILTER_TIMER` | Establishes an arbitrary timer | |
| 512 | +---------------------------+---------------------------------------------+ |
| 513 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 514 | .. attribute:: kevent.flags |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 515 | |
Georg Brandl | 1b5ab45 | 2009-08-13 07:56:35 +0000 | [diff] [blame] | 516 | Filter action. |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 517 | |
| 518 | +---------------------------+---------------------------------------------+ |
| 519 | | Constant | Meaning | |
| 520 | +===========================+=============================================+ |
| 521 | | :const:`KQ_EV_ADD` | Adds or modifies an event | |
| 522 | +---------------------------+---------------------------------------------+ |
| 523 | | :const:`KQ_EV_DELETE` | Removes an event from the queue | |
| 524 | +---------------------------+---------------------------------------------+ |
| 525 | | :const:`KQ_EV_ENABLE` | Permitscontrol() to returns the event | |
| 526 | +---------------------------+---------------------------------------------+ |
| 527 | | :const:`KQ_EV_DISABLE` | Disablesevent | |
| 528 | +---------------------------+---------------------------------------------+ |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 529 | | :const:`KQ_EV_ONESHOT` | Removes event after first occurrence | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 530 | +---------------------------+---------------------------------------------+ |
| 531 | | :const:`KQ_EV_CLEAR` | Reset the state after an event is retrieved | |
| 532 | +---------------------------+---------------------------------------------+ |
| 533 | | :const:`KQ_EV_SYSFLAGS` | internal event | |
| 534 | +---------------------------+---------------------------------------------+ |
| 535 | | :const:`KQ_EV_FLAG1` | internal event | |
| 536 | +---------------------------+---------------------------------------------+ |
| 537 | | :const:`KQ_EV_EOF` | Filter specific EOF condition | |
| 538 | +---------------------------+---------------------------------------------+ |
| 539 | | :const:`KQ_EV_ERROR` | See return values | |
| 540 | +---------------------------+---------------------------------------------+ |
| 541 | |
| 542 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 543 | .. attribute:: kevent.fflags |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 544 | |
Georg Brandl | 1b5ab45 | 2009-08-13 07:56:35 +0000 | [diff] [blame] | 545 | Filter specific flags. |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 546 | |
Georg Brandl | 1b5ab45 | 2009-08-13 07:56:35 +0000 | [diff] [blame] | 547 | :const:`KQ_FILTER_READ` and :const:`KQ_FILTER_WRITE` filter flags: |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 548 | |
| 549 | +----------------------------+--------------------------------------------+ |
| 550 | | Constant | Meaning | |
| 551 | +============================+============================================+ |
| 552 | | :const:`KQ_NOTE_LOWAT` | low water mark of a socket buffer | |
| 553 | +----------------------------+--------------------------------------------+ |
| 554 | |
Georg Brandl | 1b5ab45 | 2009-08-13 07:56:35 +0000 | [diff] [blame] | 555 | :const:`KQ_FILTER_VNODE` filter flags: |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 556 | |
| 557 | +----------------------------+--------------------------------------------+ |
| 558 | | Constant | Meaning | |
| 559 | +============================+============================================+ |
| 560 | | :const:`KQ_NOTE_DELETE` | *unlink()* was called | |
| 561 | +----------------------------+--------------------------------------------+ |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 562 | | :const:`KQ_NOTE_WRITE` | a write occurred | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 563 | +----------------------------+--------------------------------------------+ |
| 564 | | :const:`KQ_NOTE_EXTEND` | the file was extended | |
| 565 | +----------------------------+--------------------------------------------+ |
| 566 | | :const:`KQ_NOTE_ATTRIB` | an attribute was changed | |
| 567 | +----------------------------+--------------------------------------------+ |
| 568 | | :const:`KQ_NOTE_LINK` | the link count has changed | |
| 569 | +----------------------------+--------------------------------------------+ |
| 570 | | :const:`KQ_NOTE_RENAME` | the file was renamed | |
| 571 | +----------------------------+--------------------------------------------+ |
| 572 | | :const:`KQ_NOTE_REVOKE` | access to the file was revoked | |
| 573 | +----------------------------+--------------------------------------------+ |
| 574 | |
Georg Brandl | 1b5ab45 | 2009-08-13 07:56:35 +0000 | [diff] [blame] | 575 | :const:`KQ_FILTER_PROC` filter flags: |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 576 | |
| 577 | +----------------------------+--------------------------------------------+ |
| 578 | | Constant | Meaning | |
| 579 | +============================+============================================+ |
| 580 | | :const:`KQ_NOTE_EXIT` | the process has exited | |
| 581 | +----------------------------+--------------------------------------------+ |
| 582 | | :const:`KQ_NOTE_FORK` | the process has called *fork()* | |
| 583 | +----------------------------+--------------------------------------------+ |
| 584 | | :const:`KQ_NOTE_EXEC` | the process has executed a new process | |
| 585 | +----------------------------+--------------------------------------------+ |
| 586 | | :const:`KQ_NOTE_PCTRLMASK` | internal filter flag | |
| 587 | +----------------------------+--------------------------------------------+ |
| 588 | | :const:`KQ_NOTE_PDATAMASK` | internal filter flag | |
| 589 | +----------------------------+--------------------------------------------+ |
| 590 | | :const:`KQ_NOTE_TRACK` | follow a process across *fork()* | |
| 591 | +----------------------------+--------------------------------------------+ |
| 592 | | :const:`KQ_NOTE_CHILD` | returned on the child process for | |
| 593 | | | *NOTE_TRACK* | |
| 594 | +----------------------------+--------------------------------------------+ |
| 595 | | :const:`KQ_NOTE_TRACKERR` | unable to attach to a child | |
| 596 | +----------------------------+--------------------------------------------+ |
| 597 | |
Georg Brandl | 1b5ab45 | 2009-08-13 07:56:35 +0000 | [diff] [blame] | 598 | :const:`KQ_FILTER_NETDEV` filter flags (not available on Mac OS X): |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 599 | |
| 600 | +----------------------------+--------------------------------------------+ |
| 601 | | Constant | Meaning | |
| 602 | +============================+============================================+ |
| 603 | | :const:`KQ_NOTE_LINKUP` | link is up | |
| 604 | +----------------------------+--------------------------------------------+ |
| 605 | | :const:`KQ_NOTE_LINKDOWN` | link is down | |
| 606 | +----------------------------+--------------------------------------------+ |
| 607 | | :const:`KQ_NOTE_LINKINV` | link state is invalid | |
| 608 | +----------------------------+--------------------------------------------+ |
| 609 | |
| 610 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 611 | .. attribute:: kevent.data |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 612 | |
Georg Brandl | 1b5ab45 | 2009-08-13 07:56:35 +0000 | [diff] [blame] | 613 | Filter specific data. |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 614 | |
| 615 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 616 | .. attribute:: kevent.udata |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 617 | |
Georg Brandl | 1b5ab45 | 2009-08-13 07:56:35 +0000 | [diff] [blame] | 618 | User defined value. |