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