blob: 8feedaa4770c6a483095a9e22a8d385e38f1cd69 [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
Amaury Forgeot d'Arcace31022009-07-09 22:44:11 +0000174 This value is guaranteed by POSIX to be at least 512. Availability: Unix.
Gregory P. Smithb970b862009-07-04 02:28:47 +0000175
Mark Dickinson574b1d62009-10-01 20:20:09 +0000176 .. versionadded:: 3.2
Gregory P. Smithb970b862009-07-04 02:28:47 +0000177
Georg Brandl116aa622007-08-15 14:28:22 +0000178
Jesus Cead8b9ae62011-11-14 19:07:41 +0100179.. _devpoll-objects:
180
181``/dev/poll`` Polling Objects
Georg Brandl525d3552014-10-29 10:26:56 +0100182-----------------------------
Jesus Cead8b9ae62011-11-14 19:07:41 +0100183
184Solaris and derivatives have ``/dev/poll``. While :c:func:`select` is
185O(highest file descriptor) and :c:func:`poll` is O(number of file
186descriptors), ``/dev/poll`` is O(active file descriptors).
187
188``/dev/poll`` behaviour is very close to the standard :c:func:`poll`
189object.
190
191
Victor Stinner13423c32013-08-22 00:19:50 +0200192.. method:: devpoll.close()
193
194 Close the file descriptor of the polling object.
195
196 .. versionadded:: 3.4
197
198
199.. attribute:: devpoll.closed
200
201 ``True`` if the polling object is closed.
202
203 .. versionadded:: 3.4
204
205
206.. method:: devpoll.fileno()
207
208 Return the file descriptor number of the polling object.
209
210 .. versionadded:: 3.4
211
212
Jesus Cead8b9ae62011-11-14 19:07:41 +0100213.. method:: devpoll.register(fd[, eventmask])
214
215 Register a file descriptor with the polling object. Future calls to the
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300216 :meth:`poll` method will then check whether the file descriptor has any
217 pending I/O events. *fd* can be either an integer, or an object with a
218 :meth:`~io.IOBase.fileno` method that returns an integer. File objects
219 implement :meth:`!fileno`, so they can also be used as the argument.
Jesus Cead8b9ae62011-11-14 19:07:41 +0100220
221 *eventmask* is an optional bitmask describing the type of events you want to
222 check for. The constants are the same that with :c:func:`poll`
223 object. The default value is a combination of the constants :const:`POLLIN`,
224 :const:`POLLPRI`, and :const:`POLLOUT`.
225
226 .. warning::
227
228 Registering a file descriptor that's already registered is not an
Donald Stufft8b852f12014-05-20 12:58:38 -0400229 error, but the result is undefined. The appropriate action is to
Jesus Cead8b9ae62011-11-14 19:07:41 +0100230 unregister or modify it first. This is an important difference
231 compared with :c:func:`poll`.
232
233
234.. method:: devpoll.modify(fd[, eventmask])
235
236 This method does an :meth:`unregister` followed by a
237 :meth:`register`. It is (a bit) more efficient that doing the same
238 explicitly.
239
240
241.. method:: devpoll.unregister(fd)
242
243 Remove a file descriptor being tracked by a polling object. Just like the
244 :meth:`register` method, *fd* can be an integer or an object with a
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300245 :meth:`~io.IOBase.fileno` method that returns an integer.
Jesus Cead8b9ae62011-11-14 19:07:41 +0100246
247 Attempting to remove a file descriptor that was never registered is
248 safely ignored.
249
250
251.. method:: devpoll.poll([timeout])
252
253 Polls the set of registered file descriptors, and returns a possibly-empty list
254 containing ``(fd, event)`` 2-tuples for the descriptors that have events or
255 errors to report. *fd* is the file descriptor, and *event* is a bitmask with
256 bits set for the reported events for that descriptor --- :const:`POLLIN` for
257 waiting input, :const:`POLLOUT` to indicate that the descriptor can be written
258 to, and so forth. An empty list indicates that the call timed out and no file
259 descriptors had any events to report. If *timeout* is given, it specifies the
260 length of time in milliseconds which the system will wait for events before
261 returning. If *timeout* is omitted, -1, or :const:`None`, the call will
262 block until there is an event for this poll object.
263
Victor Stinner45ca48b2015-03-31 12:10:33 +0200264 .. versionchanged:: 3.5
265 The function is now retried with a recomputed timeout when interrupted by
266 a signal, except if the signal handler raises an exception (see
267 :pep:`475` for the rationale), instead of raising
268 :exc:`InterruptedError`.
269
Jesus Cead8b9ae62011-11-14 19:07:41 +0100270
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000271.. _epoll-objects:
272
273Edge and Level Trigger Polling (epoll) Objects
274----------------------------------------------
275
Sanyam Khurana1b4587a2017-12-06 22:09:33 +0530276 https://linux.die.net/man/4/epoll
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000277
278 *eventmask*
279
Benjamin Peterson0715ce32016-07-18 22:02:44 -0700280 +-------------------------+-----------------------------------------------+
281 | Constant | Meaning |
282 +=========================+===============================================+
283 | :const:`EPOLLIN` | Available for read |
284 +-------------------------+-----------------------------------------------+
285 | :const:`EPOLLOUT` | Available for write |
286 +-------------------------+-----------------------------------------------+
287 | :const:`EPOLLPRI` | Urgent data for read |
288 +-------------------------+-----------------------------------------------+
289 | :const:`EPOLLERR` | Error condition happened on the assoc. fd |
290 +-------------------------+-----------------------------------------------+
291 | :const:`EPOLLHUP` | Hang up happened on the assoc. fd |
292 +-------------------------+-----------------------------------------------+
293 | :const:`EPOLLET` | Set Edge Trigger behavior, the default is |
294 | | Level Trigger behavior |
295 +-------------------------+-----------------------------------------------+
296 | :const:`EPOLLONESHOT` | Set one-shot behavior. After one event is |
297 | | pulled out, the fd is internally disabled |
298 +-------------------------+-----------------------------------------------+
299 | :const:`EPOLLEXCLUSIVE` | Wake only one epoll object when the |
300 | | associated fd has an event. The default (if |
301 | | this flag is not set) is to wake all epoll |
NAKAMURA Osamu3e0f1fc2017-04-12 19:30:40 +0900302 | | objects polling on a fd. |
Benjamin Peterson0715ce32016-07-18 22:02:44 -0700303 +-------------------------+-----------------------------------------------+
304 | :const:`EPOLLRDHUP` | Stream socket peer closed connection or shut |
305 | | down writing half of connection. |
306 +-------------------------+-----------------------------------------------+
307 | :const:`EPOLLRDNORM` | Equivalent to :const:`EPOLLIN` |
308 +-------------------------+-----------------------------------------------+
309 | :const:`EPOLLRDBAND` | Priority data band can be read. |
310 +-------------------------+-----------------------------------------------+
311 | :const:`EPOLLWRNORM` | Equivalent to :const:`EPOLLOUT` |
312 +-------------------------+-----------------------------------------------+
313 | :const:`EPOLLWRBAND` | Priority data may be written. |
314 +-------------------------+-----------------------------------------------+
315 | :const:`EPOLLMSG` | Ignored. |
316 +-------------------------+-----------------------------------------------+
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000317
318
319.. method:: epoll.close()
320
321 Close the control file descriptor of the epoll object.
322
323
Victor Stinner13423c32013-08-22 00:19:50 +0200324.. attribute:: epoll.closed
325
326 ``True`` if the epoll object is closed.
327
328
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000329.. method:: epoll.fileno()
330
331 Return the file descriptor number of the control fd.
332
333
334.. method:: epoll.fromfd(fd)
335
336 Create an epoll object from a given file descriptor.
337
338
339.. method:: epoll.register(fd[, eventmask])
340
341 Register a fd descriptor with the epoll object.
342
343
344.. method:: epoll.modify(fd, eventmask)
345
Georg Brandl632c8122014-01-12 18:03:12 +0100346 Modify a registered file descriptor.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000347
348
349.. method:: epoll.unregister(fd)
350
351 Remove a registered file descriptor from the epoll object.
352
353
Tal Einat6dc57e22018-06-30 23:02:48 +0300354.. method:: epoll.poll(timeout=None, maxevents=-1)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000355
356 Wait for events. timeout in seconds (float)
357
Victor Stinner41eba222015-03-30 21:59:21 +0200358 .. versionchanged:: 3.5
359 The function is now retried with a recomputed timeout when interrupted by
360 a signal, except if the signal handler raises an exception (see
361 :pep:`475` for the rationale), instead of raising
362 :exc:`InterruptedError`.
363
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000364
Georg Brandl116aa622007-08-15 14:28:22 +0000365.. _poll-objects:
366
367Polling Objects
368---------------
369
Georg Brandl60203b42010-10-06 10:11:56 +0000370The :c:func:`poll` system call, supported on most Unix systems, provides better
Georg Brandl116aa622007-08-15 14:28:22 +0000371scalability for network servers that service many, many clients at the same
Georg Brandl60203b42010-10-06 10:11:56 +0000372time. :c:func:`poll` scales better because the system call only requires listing
373the file descriptors of interest, while :c:func:`select` builds a bitmap, turns
Georg Brandl116aa622007-08-15 14:28:22 +0000374on bits for the fds of interest, and then afterward the whole bitmap has to be
Georg Brandl60203b42010-10-06 10:11:56 +0000375linearly scanned again. :c:func:`select` is O(highest file descriptor), while
376:c:func:`poll` is O(number of file descriptors).
Georg Brandl116aa622007-08-15 14:28:22 +0000377
378
379.. method:: poll.register(fd[, eventmask])
380
381 Register a file descriptor with the polling object. Future calls to the
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300382 :meth:`poll` method will then check whether the file descriptor has any
383 pending I/O events. *fd* can be either an integer, or an object with a
384 :meth:`~io.IOBase.fileno` method that returns an integer. File objects
385 implement :meth:`!fileno`, so they can also be used as the argument.
Georg Brandl116aa622007-08-15 14:28:22 +0000386
387 *eventmask* is an optional bitmask describing the type of events you want to
388 check for, and can be a combination of the constants :const:`POLLIN`,
389 :const:`POLLPRI`, and :const:`POLLOUT`, described in the table below. If not
390 specified, the default value used will check for all 3 types of events.
391
392 +-------------------+------------------------------------------+
393 | Constant | Meaning |
394 +===================+==========================================+
395 | :const:`POLLIN` | There is data to read |
396 +-------------------+------------------------------------------+
397 | :const:`POLLPRI` | There is urgent data to read |
398 +-------------------+------------------------------------------+
399 | :const:`POLLOUT` | Ready for output: writing will not block |
400 +-------------------+------------------------------------------+
401 | :const:`POLLERR` | Error condition of some sort |
402 +-------------------+------------------------------------------+
403 | :const:`POLLHUP` | Hung up |
404 +-------------------+------------------------------------------+
Berker Peksagfe8d9662016-07-19 21:09:26 +0300405 | :const:`POLLRDHUP`| Stream socket peer closed connection, or |
406 | | shut down writing half of connection |
407 +-------------------+------------------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +0000408 | :const:`POLLNVAL` | Invalid request: descriptor not open |
409 +-------------------+------------------------------------------+
410
411 Registering a file descriptor that's already registered is not an error, and has
412 the same effect as registering the descriptor exactly once.
413
414
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000415.. method:: poll.modify(fd, eventmask)
416
417 Modifies an already registered fd. This has the same effect as
Jean-Paul Calderone7f94f392010-07-18 16:13:27 +0000418 ``register(fd, eventmask)``. Attempting to modify a file descriptor
Andrew Svetlov050f9ea2014-04-01 00:23:23 +0300419 that was never registered causes an :exc:`OSError` exception with errno
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000420 :const:`ENOENT` to be raised.
421
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000422
Georg Brandl116aa622007-08-15 14:28:22 +0000423.. method:: poll.unregister(fd)
424
425 Remove a file descriptor being tracked by a polling object. Just like the
426 :meth:`register` method, *fd* can be an integer or an object with a
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300427 :meth:`~io.IOBase.fileno` method that returns an integer.
Georg Brandl116aa622007-08-15 14:28:22 +0000428
429 Attempting to remove a file descriptor that was never registered causes a
430 :exc:`KeyError` exception to be raised.
431
432
433.. method:: poll.poll([timeout])
434
435 Polls the set of registered file descriptors, and returns a possibly-empty list
436 containing ``(fd, event)`` 2-tuples for the descriptors that have events or
437 errors to report. *fd* is the file descriptor, and *event* is a bitmask with
438 bits set for the reported events for that descriptor --- :const:`POLLIN` for
439 waiting input, :const:`POLLOUT` to indicate that the descriptor can be written
440 to, and so forth. An empty list indicates that the call timed out and no file
441 descriptors had any events to report. If *timeout* is given, it specifies the
442 length of time in milliseconds which the system will wait for events before
443 returning. If *timeout* is omitted, negative, or :const:`None`, the call will
444 block until there is an event for this poll object.
445
Victor Stinner3c7d6e02015-03-30 21:38:00 +0200446 .. versionchanged:: 3.5
447 The function is now retried with a recomputed timeout when interrupted by
448 a signal, except if the signal handler raises an exception (see
449 :pep:`475` for the rationale), instead of raising
450 :exc:`InterruptedError`.
451
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000452
453.. _kqueue-objects:
454
455Kqueue Objects
456--------------
457
458.. method:: kqueue.close()
459
460 Close the control file descriptor of the kqueue object.
461
462
Victor Stinner13423c32013-08-22 00:19:50 +0200463.. attribute:: kqueue.closed
464
465 ``True`` if the kqueue object is closed.
466
467
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000468.. method:: kqueue.fileno()
469
470 Return the file descriptor number of the control fd.
471
472
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000473.. method:: kqueue.fromfd(fd)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000474
475 Create a kqueue object from a given file descriptor.
476
477
Hynek Schlawack979f37a2012-05-22 16:12:18 +0200478.. method:: kqueue.control(changelist, max_events[, timeout=None]) -> eventlist
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000479
480 Low level interface to kevent
481
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300482 - changelist must be an iterable of kevent object or ``None``
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000483 - max_events must be 0 or a positive integer
484 - timeout in seconds (floats possible)
485
Victor Stinner4448c082015-03-31 11:48:34 +0200486 .. versionchanged:: 3.5
487 The function is now retried with a recomputed timeout when interrupted by
488 a signal, except if the signal handler raises an exception (see
489 :pep:`475` for the rationale), instead of raising
490 :exc:`InterruptedError`.
491
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000492
493.. _kevent-objects:
494
495Kevent Objects
496--------------
497
Serhiy Storchaka6dff0202016-05-07 10:49:07 +0300498https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000499
Christian Heimesfe337bf2008-03-23 21:54:12 +0000500.. attribute:: kevent.ident
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000501
502 Value used to identify the event. The interpretation depends on the filter
503 but it's usually the file descriptor. In the constructor ident can either
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300504 be an int or an object with a :meth:`~io.IOBase.fileno` method. kevent
505 stores the integer internally.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000506
Christian Heimesfe337bf2008-03-23 21:54:12 +0000507.. attribute:: kevent.filter
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000508
Georg Brandl1b5ab452009-08-13 07:56:35 +0000509 Name of the kernel filter.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000510
511 +---------------------------+---------------------------------------------+
512 | Constant | Meaning |
513 +===========================+=============================================+
514 | :const:`KQ_FILTER_READ` | Takes a descriptor and returns whenever |
515 | | there is data available to read |
516 +---------------------------+---------------------------------------------+
517 | :const:`KQ_FILTER_WRITE` | Takes a descriptor and returns whenever |
Georg Brandl1b5ab452009-08-13 07:56:35 +0000518 | | there is data available to write |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000519 +---------------------------+---------------------------------------------+
520 | :const:`KQ_FILTER_AIO` | AIO requests |
521 +---------------------------+---------------------------------------------+
522 | :const:`KQ_FILTER_VNODE` | Returns when one or more of the requested |
523 | | events watched in *fflag* occurs |
524 +---------------------------+---------------------------------------------+
525 | :const:`KQ_FILTER_PROC` | Watch for events on a process id |
526 +---------------------------+---------------------------------------------+
527 | :const:`KQ_FILTER_NETDEV` | Watch for events on a network device |
528 | | [not available on Mac OS X] |
529 +---------------------------+---------------------------------------------+
530 | :const:`KQ_FILTER_SIGNAL` | Returns whenever the watched signal is |
531 | | delivered to the process |
532 +---------------------------+---------------------------------------------+
533 | :const:`KQ_FILTER_TIMER` | Establishes an arbitrary timer |
534 +---------------------------+---------------------------------------------+
535
Christian Heimesfe337bf2008-03-23 21:54:12 +0000536.. attribute:: kevent.flags
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000537
Georg Brandl1b5ab452009-08-13 07:56:35 +0000538 Filter action.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000539
540 +---------------------------+---------------------------------------------+
541 | Constant | Meaning |
542 +===========================+=============================================+
543 | :const:`KQ_EV_ADD` | Adds or modifies an event |
544 +---------------------------+---------------------------------------------+
545 | :const:`KQ_EV_DELETE` | Removes an event from the queue |
546 +---------------------------+---------------------------------------------+
547 | :const:`KQ_EV_ENABLE` | Permitscontrol() to returns the event |
548 +---------------------------+---------------------------------------------+
549 | :const:`KQ_EV_DISABLE` | Disablesevent |
550 +---------------------------+---------------------------------------------+
Georg Brandl2ee470f2008-07-16 12:55:28 +0000551 | :const:`KQ_EV_ONESHOT` | Removes event after first occurrence |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000552 +---------------------------+---------------------------------------------+
553 | :const:`KQ_EV_CLEAR` | Reset the state after an event is retrieved |
554 +---------------------------+---------------------------------------------+
555 | :const:`KQ_EV_SYSFLAGS` | internal event |
556 +---------------------------+---------------------------------------------+
557 | :const:`KQ_EV_FLAG1` | internal event |
558 +---------------------------+---------------------------------------------+
559 | :const:`KQ_EV_EOF` | Filter specific EOF condition |
560 +---------------------------+---------------------------------------------+
561 | :const:`KQ_EV_ERROR` | See return values |
562 +---------------------------+---------------------------------------------+
563
564
Christian Heimesfe337bf2008-03-23 21:54:12 +0000565.. attribute:: kevent.fflags
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000566
Georg Brandl1b5ab452009-08-13 07:56:35 +0000567 Filter specific flags.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000568
Georg Brandl1b5ab452009-08-13 07:56:35 +0000569 :const:`KQ_FILTER_READ` and :const:`KQ_FILTER_WRITE` filter flags:
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000570
571 +----------------------------+--------------------------------------------+
572 | Constant | Meaning |
573 +============================+============================================+
574 | :const:`KQ_NOTE_LOWAT` | low water mark of a socket buffer |
575 +----------------------------+--------------------------------------------+
576
Georg Brandl1b5ab452009-08-13 07:56:35 +0000577 :const:`KQ_FILTER_VNODE` filter flags:
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000578
579 +----------------------------+--------------------------------------------+
580 | Constant | Meaning |
581 +============================+============================================+
582 | :const:`KQ_NOTE_DELETE` | *unlink()* was called |
583 +----------------------------+--------------------------------------------+
Georg Brandl2ee470f2008-07-16 12:55:28 +0000584 | :const:`KQ_NOTE_WRITE` | a write occurred |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000585 +----------------------------+--------------------------------------------+
586 | :const:`KQ_NOTE_EXTEND` | the file was extended |
587 +----------------------------+--------------------------------------------+
588 | :const:`KQ_NOTE_ATTRIB` | an attribute was changed |
589 +----------------------------+--------------------------------------------+
590 | :const:`KQ_NOTE_LINK` | the link count has changed |
591 +----------------------------+--------------------------------------------+
592 | :const:`KQ_NOTE_RENAME` | the file was renamed |
593 +----------------------------+--------------------------------------------+
594 | :const:`KQ_NOTE_REVOKE` | access to the file was revoked |
595 +----------------------------+--------------------------------------------+
596
Georg Brandl1b5ab452009-08-13 07:56:35 +0000597 :const:`KQ_FILTER_PROC` filter flags:
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000598
599 +----------------------------+--------------------------------------------+
600 | Constant | Meaning |
601 +============================+============================================+
602 | :const:`KQ_NOTE_EXIT` | the process has exited |
603 +----------------------------+--------------------------------------------+
604 | :const:`KQ_NOTE_FORK` | the process has called *fork()* |
605 +----------------------------+--------------------------------------------+
606 | :const:`KQ_NOTE_EXEC` | the process has executed a new process |
607 +----------------------------+--------------------------------------------+
608 | :const:`KQ_NOTE_PCTRLMASK` | internal filter flag |
609 +----------------------------+--------------------------------------------+
610 | :const:`KQ_NOTE_PDATAMASK` | internal filter flag |
611 +----------------------------+--------------------------------------------+
612 | :const:`KQ_NOTE_TRACK` | follow a process across *fork()* |
613 +----------------------------+--------------------------------------------+
614 | :const:`KQ_NOTE_CHILD` | returned on the child process for |
615 | | *NOTE_TRACK* |
616 +----------------------------+--------------------------------------------+
617 | :const:`KQ_NOTE_TRACKERR` | unable to attach to a child |
618 +----------------------------+--------------------------------------------+
619
Georg Brandl1b5ab452009-08-13 07:56:35 +0000620 :const:`KQ_FILTER_NETDEV` filter flags (not available on Mac OS X):
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000621
622 +----------------------------+--------------------------------------------+
623 | Constant | Meaning |
624 +============================+============================================+
625 | :const:`KQ_NOTE_LINKUP` | link is up |
626 +----------------------------+--------------------------------------------+
627 | :const:`KQ_NOTE_LINKDOWN` | link is down |
628 +----------------------------+--------------------------------------------+
629 | :const:`KQ_NOTE_LINKINV` | link state is invalid |
630 +----------------------------+--------------------------------------------+
631
632
Christian Heimesfe337bf2008-03-23 21:54:12 +0000633.. attribute:: kevent.data
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000634
Georg Brandl1b5ab452009-08-13 07:56:35 +0000635 Filter specific data.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000636
637
Christian Heimesfe337bf2008-03-23 21:54:12 +0000638.. attribute:: kevent.udata
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000639
Georg Brandl1b5ab452009-08-13 07:56:35 +0000640 User defined value.