blob: 733a91e20b684d33cbde7fd9a81a37aafb295b7a [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001: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 Reedyfa089b92016-06-11 15:02:54 -04007--------------
Georg Brandl116aa622007-08-15 14:28:22 +00008
Georg Brandl60203b42010-10-06 10:11:56 +00009This module provides access to the :c:func:`select` and :c:func:`poll` functions
Jesus Cead8b9ae62011-11-14 19:07:41 +010010available in most operating systems, :c:func:`devpoll` available on
11Solaris and derivatives, :c:func:`epoll` available on Linux 2.5+ and
Georg Brandl60203b42010-10-06 10:11:56 +000012:c:func:`kqueue` available on most BSD.
Christian Heimes4fbc72b2008-03-22 00:47:35 +000013Note that on Windows, it only works for sockets; on other operating systems,
14it also works for other file types (in particular, on Unix, it works on pipes).
15It cannot be used on regular files to determine whether a file has grown since
16it was last read.
Georg Brandl116aa622007-08-15 14:28:22 +000017
Victor Stinner73821c42013-09-04 20:40:13 +020018.. 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 Brandl116aa622007-08-15 14:28:22 +000026The module defines the following:
27
28
29.. exception:: error
30
Antoine Pitrou9b7fcf82011-10-12 16:23:02 +020031 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 Brandl116aa622007-08-15 14:28:22 +000035
36
Jesus Cead8b9ae62011-11-14 19:07:41 +010037.. function:: devpoll()
Jesus Ceaf450c1b2011-11-15 05:42:59 +010038
Jesus Cead8b9ae62011-11-14 19:07:41 +010039 (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 Ceaf450c1b2011-11-15 05:42:59 +010046 increases this value, :c:func:`devpoll` may return an
Jesus Cead8b9ae62011-11-14 19:07:41 +010047 incomplete list of active file descriptors.
48
Victor Stinnerdaf45552013-08-28 00:53:59 +020049 The new file descriptor is :ref:`non-inheritable <fd_inheritance>`.
50
Jesus Cead8b9ae62011-11-14 19:07:41 +010051 .. versionadded:: 3.3
52
Victor Stinnerdaf45552013-08-28 00:53:59 +020053 .. versionchanged:: 3.4
54 The new file descriptor is now non-inheritable.
55
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -060056.. function:: epoll(sizehint=-1, flags=0)
Christian Heimes4fbc72b2008-03-22 00:47:35 +000057
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -060058 (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
Tal Einat0cdf5f42018-06-30 15:43:23 +030060 events.
61
62 *sizehint* informs epoll about the expected number of events to be
63 registered. It must be positive, or `-1` to use the default. It is only
64 used on older systems where :c:func:`epoll_create1` is not available;
65 otherwise it has no effect (though its value is still checked).
66
67 *flags* is deprecated and completely ignored. However, when supplied, its
68 value must be ``0`` or ``select.EPOLL_CLOEXEC``, otherwise ``OSError`` is
69 raised.
R David Murray2bc930f2013-12-31 11:17:21 -050070
71 See the :ref:`epoll-objects` section below for the methods supported by
72 epolling objects.
73
74 ``epoll`` objects support the context management protocol: when used in a
75 :keyword:`with` statement, the new file descriptor is automatically closed
76 at the end of the block.
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -060077
Victor Stinnerdaf45552013-08-28 00:53:59 +020078 The new file descriptor is :ref:`non-inheritable <fd_inheritance>`.
79
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -060080 .. versionchanged:: 3.3
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -060081 Added the *flags* parameter.
Christian Heimes4fbc72b2008-03-22 00:47:35 +000082
Antoine Pitrou09bb89b2012-12-15 21:14:21 +010083 .. versionchanged:: 3.4
84 Support for the :keyword:`with` statement was added.
Victor Stinnerdaf45552013-08-28 00:53:59 +020085 The new file descriptor is now non-inheritable.
Antoine Pitrou09bb89b2012-12-15 21:14:21 +010086
Berker Peksag1c697a52016-09-11 15:45:32 +030087 .. deprecated:: 3.4
88 The *flags* parameter. ``select.EPOLL_CLOEXEC`` is used by default now.
89 Use :func:`os.set_inheritable` to make the file descriptor inheritable.
90
Christian Heimes4fbc72b2008-03-22 00:47:35 +000091
Georg Brandl116aa622007-08-15 14:28:22 +000092.. function:: poll()
93
94 (Not supported by all operating systems.) Returns a polling object, which
95 supports registering and unregistering file descriptors, and then polling them
96 for I/O events; see section :ref:`poll-objects` below for the methods supported
97 by polling objects.
98
99
Christian Heimesfe337bf2008-03-23 21:54:12 +0000100.. function:: kqueue()
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000101
Georg Brandle767e042010-07-14 08:00:22 +0000102 (Only supported on BSD.) Returns a kernel queue object; see section
Christian Heimesfe337bf2008-03-23 21:54:12 +0000103 :ref:`kqueue-objects` below for the methods supported by kqueue objects.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000104
Victor Stinnerdaf45552013-08-28 00:53:59 +0200105 The new file descriptor is :ref:`non-inheritable <fd_inheritance>`.
106
107 .. versionchanged:: 3.4
108 The new file descriptor is now non-inheritable.
109
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000110
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000111.. function:: kevent(ident, filter=KQ_FILTER_READ, flags=KQ_EV_ADD, fflags=0, data=0, udata=0)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000112
Georg Brandle767e042010-07-14 08:00:22 +0000113 (Only supported on BSD.) Returns a kernel event object; see section
114 :ref:`kevent-objects` below for the methods supported by kevent objects.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000115
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000116
Georg Brandl734e2682008-08-12 08:18:18 +0000117.. function:: select(rlist, wlist, xlist[, timeout])
Georg Brandl116aa622007-08-15 14:28:22 +0000118
Georg Brandl60203b42010-10-06 10:11:56 +0000119 This is a straightforward interface to the Unix :c:func:`select` system call.
Georg Brandl116aa622007-08-15 14:28:22 +0000120 The first three arguments are sequences of 'waitable objects': either
121 integers representing file descriptors or objects with a parameterless method
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300122 named :meth:`~io.IOBase.fileno` returning such an integer:
Georg Brandl734e2682008-08-12 08:18:18 +0000123
124 * *rlist*: wait until ready for reading
125 * *wlist*: wait until ready for writing
126 * *xlist*: wait for an "exceptional condition" (see the manual page for what
127 your system considers such a condition)
128
129 Empty sequences are allowed, but acceptance of three empty sequences is
130 platform-dependent. (It is known to work on Unix but not on Windows.) The
131 optional *timeout* argument specifies a time-out as a floating point number
132 in seconds. When the *timeout* argument is omitted the function blocks until
133 at least one file descriptor is ready. A time-out value of zero specifies a
134 poll and never blocks.
Georg Brandl116aa622007-08-15 14:28:22 +0000135
136 The return value is a triple of lists of objects that are ready: subsets of the
137 first three arguments. When the time-out is reached without a file descriptor
138 becoming ready, three empty lists are returned.
139
140 .. index::
141 single: socket() (in module socket)
142 single: popen() (in module os)
143
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000144 Among the acceptable object types in the sequences are Python :term:`file
145 objects <file object>` (e.g. ``sys.stdin``, or objects returned by
146 :func:`open` or :func:`os.popen`), socket objects returned by
147 :func:`socket.socket`. You may also define a :dfn:`wrapper` class yourself,
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300148 as long as it has an appropriate :meth:`~io.IOBase.fileno` method (that
149 really returns a file descriptor, not just a random integer).
Georg Brandl116aa622007-08-15 14:28:22 +0000150
Georg Brandl116aa622007-08-15 14:28:22 +0000151 .. note::
152
153 .. index:: single: WinSock
154
Georg Brandl734e2682008-08-12 08:18:18 +0000155 File objects on Windows are not acceptable, but sockets are. On Windows,
Georg Brandl60203b42010-10-06 10:11:56 +0000156 the underlying :c:func:`select` function is provided by the WinSock
Georg Brandl734e2682008-08-12 08:18:18 +0000157 library, and does not handle file descriptors that don't originate from
158 WinSock.
Georg Brandl116aa622007-08-15 14:28:22 +0000159
Victor Stinnerf70e1ca2015-03-30 21:16:11 +0200160 .. versionchanged:: 3.5
161 The function is now retried with a recomputed timeout when interrupted by
162 a signal, except if the signal handler raises an exception (see
163 :pep:`475` for the rationale), instead of raising
164 :exc:`InterruptedError`.
165
166
Antoine Pitroucfad97b2011-01-25 17:24:57 +0000167.. attribute:: PIPE_BUF
Gregory P. Smithb970b862009-07-04 02:28:47 +0000168
Antoine Pitrouda7be3e2011-01-25 16:28:44 +0000169 The minimum number of bytes which can be written without blocking to a pipe
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300170 when the pipe has been reported as ready for writing by :func:`~select.select`,
Antoine Pitrouda7be3e2011-01-25 16:28:44 +0000171 :func:`poll` or another interface in this module. This doesn't apply
172 to other kind of file-like objects such as sockets.
173
Cheryl Sabella2d6097d2018-10-12 10:55:20 -0400174 This value is guaranteed by POSIX to be at least 512.
175
176 .. availability:: Unix
Gregory P. Smithb970b862009-07-04 02:28:47 +0000177
Mark Dickinson574b1d62009-10-01 20:20:09 +0000178 .. versionadded:: 3.2
Gregory P. Smithb970b862009-07-04 02:28:47 +0000179
Georg Brandl116aa622007-08-15 14:28:22 +0000180
Jesus Cead8b9ae62011-11-14 19:07:41 +0100181.. _devpoll-objects:
182
183``/dev/poll`` Polling Objects
Georg Brandl525d3552014-10-29 10:26:56 +0100184-----------------------------
Jesus Cead8b9ae62011-11-14 19:07:41 +0100185
186Solaris and derivatives have ``/dev/poll``. While :c:func:`select` is
187O(highest file descriptor) and :c:func:`poll` is O(number of file
188descriptors), ``/dev/poll`` is O(active file descriptors).
189
190``/dev/poll`` behaviour is very close to the standard :c:func:`poll`
191object.
192
193
Victor Stinner13423c32013-08-22 00:19:50 +0200194.. method:: devpoll.close()
195
196 Close the file descriptor of the polling object.
197
198 .. versionadded:: 3.4
199
200
201.. attribute:: devpoll.closed
202
203 ``True`` if the polling object is closed.
204
205 .. versionadded:: 3.4
206
207
208.. method:: devpoll.fileno()
209
210 Return the file descriptor number of the polling object.
211
212 .. versionadded:: 3.4
213
214
Jesus Cead8b9ae62011-11-14 19:07:41 +0100215.. method:: devpoll.register(fd[, eventmask])
216
217 Register a file descriptor with the polling object. Future calls to the
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300218 :meth:`poll` method will then check whether the file descriptor has any
219 pending I/O events. *fd* can be either an integer, or an object with a
220 :meth:`~io.IOBase.fileno` method that returns an integer. File objects
221 implement :meth:`!fileno`, so they can also be used as the argument.
Jesus Cead8b9ae62011-11-14 19:07:41 +0100222
223 *eventmask* is an optional bitmask describing the type of events you want to
224 check for. The constants are the same that with :c:func:`poll`
225 object. The default value is a combination of the constants :const:`POLLIN`,
226 :const:`POLLPRI`, and :const:`POLLOUT`.
227
228 .. warning::
229
230 Registering a file descriptor that's already registered is not an
Donald Stufft8b852f12014-05-20 12:58:38 -0400231 error, but the result is undefined. The appropriate action is to
Jesus Cead8b9ae62011-11-14 19:07:41 +0100232 unregister or modify it first. This is an important difference
233 compared with :c:func:`poll`.
234
235
236.. method:: devpoll.modify(fd[, eventmask])
237
238 This method does an :meth:`unregister` followed by a
239 :meth:`register`. It is (a bit) more efficient that doing the same
240 explicitly.
241
242
243.. method:: devpoll.unregister(fd)
244
245 Remove a file descriptor being tracked by a polling object. Just like the
246 :meth:`register` method, *fd* can be an integer or an object with a
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300247 :meth:`~io.IOBase.fileno` method that returns an integer.
Jesus Cead8b9ae62011-11-14 19:07:41 +0100248
249 Attempting to remove a file descriptor that was never registered is
250 safely ignored.
251
252
253.. method:: devpoll.poll([timeout])
254
255 Polls the set of registered file descriptors, and returns a possibly-empty list
256 containing ``(fd, event)`` 2-tuples for the descriptors that have events or
257 errors to report. *fd* is the file descriptor, and *event* is a bitmask with
258 bits set for the reported events for that descriptor --- :const:`POLLIN` for
259 waiting input, :const:`POLLOUT` to indicate that the descriptor can be written
260 to, and so forth. An empty list indicates that the call timed out and no file
261 descriptors had any events to report. If *timeout* is given, it specifies the
262 length of time in milliseconds which the system will wait for events before
263 returning. If *timeout* is omitted, -1, or :const:`None`, the call will
264 block until there is an event for this poll object.
265
Victor Stinner45ca48b2015-03-31 12:10:33 +0200266 .. versionchanged:: 3.5
267 The function is now retried with a recomputed timeout when interrupted by
268 a signal, except if the signal handler raises an exception (see
269 :pep:`475` for the rationale), instead of raising
270 :exc:`InterruptedError`.
271
Jesus Cead8b9ae62011-11-14 19:07:41 +0100272
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000273.. _epoll-objects:
274
275Edge and Level Trigger Polling (epoll) Objects
276----------------------------------------------
277
Sanyam Khurana1b4587a2017-12-06 22:09:33 +0530278 https://linux.die.net/man/4/epoll
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000279
280 *eventmask*
281
Benjamin Peterson0715ce32016-07-18 22:02:44 -0700282 +-------------------------+-----------------------------------------------+
283 | Constant | Meaning |
284 +=========================+===============================================+
285 | :const:`EPOLLIN` | Available for read |
286 +-------------------------+-----------------------------------------------+
287 | :const:`EPOLLOUT` | Available for write |
288 +-------------------------+-----------------------------------------------+
289 | :const:`EPOLLPRI` | Urgent data for read |
290 +-------------------------+-----------------------------------------------+
291 | :const:`EPOLLERR` | Error condition happened on the assoc. fd |
292 +-------------------------+-----------------------------------------------+
293 | :const:`EPOLLHUP` | Hang up happened on the assoc. fd |
294 +-------------------------+-----------------------------------------------+
295 | :const:`EPOLLET` | Set Edge Trigger behavior, the default is |
296 | | Level Trigger behavior |
297 +-------------------------+-----------------------------------------------+
298 | :const:`EPOLLONESHOT` | Set one-shot behavior. After one event is |
299 | | pulled out, the fd is internally disabled |
300 +-------------------------+-----------------------------------------------+
301 | :const:`EPOLLEXCLUSIVE` | Wake only one epoll object when the |
302 | | associated fd has an event. The default (if |
303 | | this flag is not set) is to wake all epoll |
NAKAMURA Osamu3e0f1fc2017-04-12 19:30:40 +0900304 | | objects polling on a fd. |
Benjamin Peterson0715ce32016-07-18 22:02:44 -0700305 +-------------------------+-----------------------------------------------+
306 | :const:`EPOLLRDHUP` | Stream socket peer closed connection or shut |
307 | | down writing half of connection. |
308 +-------------------------+-----------------------------------------------+
309 | :const:`EPOLLRDNORM` | Equivalent to :const:`EPOLLIN` |
310 +-------------------------+-----------------------------------------------+
311 | :const:`EPOLLRDBAND` | Priority data band can be read. |
312 +-------------------------+-----------------------------------------------+
313 | :const:`EPOLLWRNORM` | Equivalent to :const:`EPOLLOUT` |
314 +-------------------------+-----------------------------------------------+
315 | :const:`EPOLLWRBAND` | Priority data may be written. |
316 +-------------------------+-----------------------------------------------+
317 | :const:`EPOLLMSG` | Ignored. |
318 +-------------------------+-----------------------------------------------+
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000319
Manjusaka92330c02018-12-19 19:59:52 +0800320 .. versionadded:: 3.6
321 :const:`EPOLLEXCLUSIVE` was added. It's only supported by Linux Kernel 4.5
322 or later.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000323
324.. method:: epoll.close()
325
326 Close the control file descriptor of the epoll object.
327
328
Victor Stinner13423c32013-08-22 00:19:50 +0200329.. attribute:: epoll.closed
330
331 ``True`` if the epoll object is closed.
332
333
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000334.. method:: epoll.fileno()
335
336 Return the file descriptor number of the control fd.
337
338
339.. method:: epoll.fromfd(fd)
340
341 Create an epoll object from a given file descriptor.
342
343
344.. method:: epoll.register(fd[, eventmask])
345
346 Register a fd descriptor with the epoll object.
347
348
349.. method:: epoll.modify(fd, eventmask)
350
Georg Brandl632c8122014-01-12 18:03:12 +0100351 Modify a registered file descriptor.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000352
353
354.. method:: epoll.unregister(fd)
355
356 Remove a registered file descriptor from the epoll object.
357
358
Tal Einat6dc57e22018-06-30 23:02:48 +0300359.. method:: epoll.poll(timeout=None, maxevents=-1)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000360
361 Wait for events. timeout in seconds (float)
362
Victor Stinner41eba222015-03-30 21:59:21 +0200363 .. versionchanged:: 3.5
364 The function is now retried with a recomputed timeout when interrupted by
365 a signal, except if the signal handler raises an exception (see
366 :pep:`475` for the rationale), instead of raising
367 :exc:`InterruptedError`.
368
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000369
Georg Brandl116aa622007-08-15 14:28:22 +0000370.. _poll-objects:
371
372Polling Objects
373---------------
374
Georg Brandl60203b42010-10-06 10:11:56 +0000375The :c:func:`poll` system call, supported on most Unix systems, provides better
Georg Brandl116aa622007-08-15 14:28:22 +0000376scalability for network servers that service many, many clients at the same
Georg Brandl60203b42010-10-06 10:11:56 +0000377time. :c:func:`poll` scales better because the system call only requires listing
378the file descriptors of interest, while :c:func:`select` builds a bitmap, turns
Georg Brandl116aa622007-08-15 14:28:22 +0000379on bits for the fds of interest, and then afterward the whole bitmap has to be
Georg Brandl60203b42010-10-06 10:11:56 +0000380linearly scanned again. :c:func:`select` is O(highest file descriptor), while
381:c:func:`poll` is O(number of file descriptors).
Georg Brandl116aa622007-08-15 14:28:22 +0000382
383
384.. method:: poll.register(fd[, eventmask])
385
386 Register a file descriptor with the polling object. Future calls to the
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300387 :meth:`poll` method will then check whether the file descriptor has any
388 pending I/O events. *fd* can be either an integer, or an object with a
389 :meth:`~io.IOBase.fileno` method that returns an integer. File objects
390 implement :meth:`!fileno`, so they can also be used as the argument.
Georg Brandl116aa622007-08-15 14:28:22 +0000391
392 *eventmask* is an optional bitmask describing the type of events you want to
393 check for, and can be a combination of the constants :const:`POLLIN`,
394 :const:`POLLPRI`, and :const:`POLLOUT`, described in the table below. If not
395 specified, the default value used will check for all 3 types of events.
396
397 +-------------------+------------------------------------------+
398 | Constant | Meaning |
399 +===================+==========================================+
400 | :const:`POLLIN` | There is data to read |
401 +-------------------+------------------------------------------+
402 | :const:`POLLPRI` | There is urgent data to read |
403 +-------------------+------------------------------------------+
404 | :const:`POLLOUT` | Ready for output: writing will not block |
405 +-------------------+------------------------------------------+
406 | :const:`POLLERR` | Error condition of some sort |
407 +-------------------+------------------------------------------+
408 | :const:`POLLHUP` | Hung up |
409 +-------------------+------------------------------------------+
Berker Peksagfe8d9662016-07-19 21:09:26 +0300410 | :const:`POLLRDHUP`| Stream socket peer closed connection, or |
411 | | shut down writing half of connection |
412 +-------------------+------------------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +0000413 | :const:`POLLNVAL` | Invalid request: descriptor not open |
414 +-------------------+------------------------------------------+
415
416 Registering a file descriptor that's already registered is not an error, and has
417 the same effect as registering the descriptor exactly once.
418
419
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000420.. method:: poll.modify(fd, eventmask)
421
422 Modifies an already registered fd. This has the same effect as
Jean-Paul Calderone7f94f392010-07-18 16:13:27 +0000423 ``register(fd, eventmask)``. Attempting to modify a file descriptor
Andrew Svetlov050f9ea2014-04-01 00:23:23 +0300424 that was never registered causes an :exc:`OSError` exception with errno
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000425 :const:`ENOENT` to be raised.
426
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000427
Georg Brandl116aa622007-08-15 14:28:22 +0000428.. method:: poll.unregister(fd)
429
430 Remove a file descriptor being tracked by a polling object. Just like the
431 :meth:`register` method, *fd* can be an integer or an object with a
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300432 :meth:`~io.IOBase.fileno` method that returns an integer.
Georg Brandl116aa622007-08-15 14:28:22 +0000433
434 Attempting to remove a file descriptor that was never registered causes a
435 :exc:`KeyError` exception to be raised.
436
437
438.. method:: poll.poll([timeout])
439
440 Polls the set of registered file descriptors, and returns a possibly-empty list
441 containing ``(fd, event)`` 2-tuples for the descriptors that have events or
442 errors to report. *fd* is the file descriptor, and *event* is a bitmask with
443 bits set for the reported events for that descriptor --- :const:`POLLIN` for
444 waiting input, :const:`POLLOUT` to indicate that the descriptor can be written
445 to, and so forth. An empty list indicates that the call timed out and no file
446 descriptors had any events to report. If *timeout* is given, it specifies the
447 length of time in milliseconds which the system will wait for events before
448 returning. If *timeout* is omitted, negative, or :const:`None`, the call will
449 block until there is an event for this poll object.
450
Victor Stinner3c7d6e02015-03-30 21:38:00 +0200451 .. versionchanged:: 3.5
452 The function is now retried with a recomputed timeout when interrupted by
453 a signal, except if the signal handler raises an exception (see
454 :pep:`475` for the rationale), instead of raising
455 :exc:`InterruptedError`.
456
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000457
458.. _kqueue-objects:
459
460Kqueue Objects
461--------------
462
463.. method:: kqueue.close()
464
465 Close the control file descriptor of the kqueue object.
466
467
Victor Stinner13423c32013-08-22 00:19:50 +0200468.. attribute:: kqueue.closed
469
470 ``True`` if the kqueue object is closed.
471
472
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000473.. method:: kqueue.fileno()
474
475 Return the file descriptor number of the control fd.
476
477
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000478.. method:: kqueue.fromfd(fd)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000479
480 Create a kqueue object from a given file descriptor.
481
482
Hynek Schlawack979f37a2012-05-22 16:12:18 +0200483.. method:: kqueue.control(changelist, max_events[, timeout=None]) -> eventlist
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000484
485 Low level interface to kevent
486
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300487 - changelist must be an iterable of kevent object or ``None``
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000488 - max_events must be 0 or a positive integer
489 - timeout in seconds (floats possible)
490
Victor Stinner4448c082015-03-31 11:48:34 +0200491 .. versionchanged:: 3.5
492 The function is now retried with a recomputed timeout when interrupted by
493 a signal, except if the signal handler raises an exception (see
494 :pep:`475` for the rationale), instead of raising
495 :exc:`InterruptedError`.
496
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000497
498.. _kevent-objects:
499
500Kevent Objects
501--------------
502
Serhiy Storchaka6dff0202016-05-07 10:49:07 +0300503https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000504
Christian Heimesfe337bf2008-03-23 21:54:12 +0000505.. attribute:: kevent.ident
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000506
507 Value used to identify the event. The interpretation depends on the filter
508 but it's usually the file descriptor. In the constructor ident can either
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300509 be an int or an object with a :meth:`~io.IOBase.fileno` method. kevent
510 stores the integer internally.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000511
Christian Heimesfe337bf2008-03-23 21:54:12 +0000512.. attribute:: kevent.filter
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000513
Georg Brandl1b5ab452009-08-13 07:56:35 +0000514 Name of the kernel filter.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000515
516 +---------------------------+---------------------------------------------+
517 | Constant | Meaning |
518 +===========================+=============================================+
519 | :const:`KQ_FILTER_READ` | Takes a descriptor and returns whenever |
520 | | there is data available to read |
521 +---------------------------+---------------------------------------------+
522 | :const:`KQ_FILTER_WRITE` | Takes a descriptor and returns whenever |
Georg Brandl1b5ab452009-08-13 07:56:35 +0000523 | | there is data available to write |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000524 +---------------------------+---------------------------------------------+
525 | :const:`KQ_FILTER_AIO` | AIO requests |
526 +---------------------------+---------------------------------------------+
527 | :const:`KQ_FILTER_VNODE` | Returns when one or more of the requested |
528 | | events watched in *fflag* occurs |
529 +---------------------------+---------------------------------------------+
530 | :const:`KQ_FILTER_PROC` | Watch for events on a process id |
531 +---------------------------+---------------------------------------------+
532 | :const:`KQ_FILTER_NETDEV` | Watch for events on a network device |
533 | | [not available on Mac OS X] |
534 +---------------------------+---------------------------------------------+
535 | :const:`KQ_FILTER_SIGNAL` | Returns whenever the watched signal is |
536 | | delivered to the process |
537 +---------------------------+---------------------------------------------+
538 | :const:`KQ_FILTER_TIMER` | Establishes an arbitrary timer |
539 +---------------------------+---------------------------------------------+
540
Christian Heimesfe337bf2008-03-23 21:54:12 +0000541.. attribute:: kevent.flags
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000542
Georg Brandl1b5ab452009-08-13 07:56:35 +0000543 Filter action.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000544
545 +---------------------------+---------------------------------------------+
546 | Constant | Meaning |
547 +===========================+=============================================+
548 | :const:`KQ_EV_ADD` | Adds or modifies an event |
549 +---------------------------+---------------------------------------------+
550 | :const:`KQ_EV_DELETE` | Removes an event from the queue |
551 +---------------------------+---------------------------------------------+
552 | :const:`KQ_EV_ENABLE` | Permitscontrol() to returns the event |
553 +---------------------------+---------------------------------------------+
554 | :const:`KQ_EV_DISABLE` | Disablesevent |
555 +---------------------------+---------------------------------------------+
Georg Brandl2ee470f2008-07-16 12:55:28 +0000556 | :const:`KQ_EV_ONESHOT` | Removes event after first occurrence |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000557 +---------------------------+---------------------------------------------+
558 | :const:`KQ_EV_CLEAR` | Reset the state after an event is retrieved |
559 +---------------------------+---------------------------------------------+
560 | :const:`KQ_EV_SYSFLAGS` | internal event |
561 +---------------------------+---------------------------------------------+
562 | :const:`KQ_EV_FLAG1` | internal event |
563 +---------------------------+---------------------------------------------+
564 | :const:`KQ_EV_EOF` | Filter specific EOF condition |
565 +---------------------------+---------------------------------------------+
566 | :const:`KQ_EV_ERROR` | See return values |
567 +---------------------------+---------------------------------------------+
568
569
Christian Heimesfe337bf2008-03-23 21:54:12 +0000570.. attribute:: kevent.fflags
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000571
Georg Brandl1b5ab452009-08-13 07:56:35 +0000572 Filter specific flags.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000573
Georg Brandl1b5ab452009-08-13 07:56:35 +0000574 :const:`KQ_FILTER_READ` and :const:`KQ_FILTER_WRITE` filter flags:
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000575
576 +----------------------------+--------------------------------------------+
577 | Constant | Meaning |
578 +============================+============================================+
579 | :const:`KQ_NOTE_LOWAT` | low water mark of a socket buffer |
580 +----------------------------+--------------------------------------------+
581
Georg Brandl1b5ab452009-08-13 07:56:35 +0000582 :const:`KQ_FILTER_VNODE` filter flags:
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000583
584 +----------------------------+--------------------------------------------+
585 | Constant | Meaning |
586 +============================+============================================+
587 | :const:`KQ_NOTE_DELETE` | *unlink()* was called |
588 +----------------------------+--------------------------------------------+
Georg Brandl2ee470f2008-07-16 12:55:28 +0000589 | :const:`KQ_NOTE_WRITE` | a write occurred |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000590 +----------------------------+--------------------------------------------+
591 | :const:`KQ_NOTE_EXTEND` | the file was extended |
592 +----------------------------+--------------------------------------------+
593 | :const:`KQ_NOTE_ATTRIB` | an attribute was changed |
594 +----------------------------+--------------------------------------------+
595 | :const:`KQ_NOTE_LINK` | the link count has changed |
596 +----------------------------+--------------------------------------------+
597 | :const:`KQ_NOTE_RENAME` | the file was renamed |
598 +----------------------------+--------------------------------------------+
599 | :const:`KQ_NOTE_REVOKE` | access to the file was revoked |
600 +----------------------------+--------------------------------------------+
601
Georg Brandl1b5ab452009-08-13 07:56:35 +0000602 :const:`KQ_FILTER_PROC` filter flags:
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000603
604 +----------------------------+--------------------------------------------+
605 | Constant | Meaning |
606 +============================+============================================+
607 | :const:`KQ_NOTE_EXIT` | the process has exited |
608 +----------------------------+--------------------------------------------+
609 | :const:`KQ_NOTE_FORK` | the process has called *fork()* |
610 +----------------------------+--------------------------------------------+
611 | :const:`KQ_NOTE_EXEC` | the process has executed a new process |
612 +----------------------------+--------------------------------------------+
613 | :const:`KQ_NOTE_PCTRLMASK` | internal filter flag |
614 +----------------------------+--------------------------------------------+
615 | :const:`KQ_NOTE_PDATAMASK` | internal filter flag |
616 +----------------------------+--------------------------------------------+
617 | :const:`KQ_NOTE_TRACK` | follow a process across *fork()* |
618 +----------------------------+--------------------------------------------+
619 | :const:`KQ_NOTE_CHILD` | returned on the child process for |
620 | | *NOTE_TRACK* |
621 +----------------------------+--------------------------------------------+
622 | :const:`KQ_NOTE_TRACKERR` | unable to attach to a child |
623 +----------------------------+--------------------------------------------+
624
Georg Brandl1b5ab452009-08-13 07:56:35 +0000625 :const:`KQ_FILTER_NETDEV` filter flags (not available on Mac OS X):
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000626
627 +----------------------------+--------------------------------------------+
628 | Constant | Meaning |
629 +============================+============================================+
630 | :const:`KQ_NOTE_LINKUP` | link is up |
631 +----------------------------+--------------------------------------------+
632 | :const:`KQ_NOTE_LINKDOWN` | link is down |
633 +----------------------------+--------------------------------------------+
634 | :const:`KQ_NOTE_LINKINV` | link state is invalid |
635 +----------------------------+--------------------------------------------+
636
637
Christian Heimesfe337bf2008-03-23 21:54:12 +0000638.. attribute:: kevent.data
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000639
Georg Brandl1b5ab452009-08-13 07:56:35 +0000640 Filter specific data.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000641
642
Christian Heimesfe337bf2008-03-23 21:54:12 +0000643.. attribute:: kevent.udata
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000644
Georg Brandl1b5ab452009-08-13 07:56:35 +0000645 User defined value.