blob: f5b25db774f6549cdbce197e5e550418d2a5e16a [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
320
321.. method:: epoll.close()
322
323 Close the control file descriptor of the epoll object.
324
325
Victor Stinner13423c32013-08-22 00:19:50 +0200326.. attribute:: epoll.closed
327
328 ``True`` if the epoll object is closed.
329
330
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000331.. method:: epoll.fileno()
332
333 Return the file descriptor number of the control fd.
334
335
336.. method:: epoll.fromfd(fd)
337
338 Create an epoll object from a given file descriptor.
339
340
341.. method:: epoll.register(fd[, eventmask])
342
343 Register a fd descriptor with the epoll object.
344
345
346.. method:: epoll.modify(fd, eventmask)
347
Georg Brandl632c8122014-01-12 18:03:12 +0100348 Modify a registered file descriptor.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000349
350
351.. method:: epoll.unregister(fd)
352
353 Remove a registered file descriptor from the epoll object.
354
355
Tal Einat6dc57e22018-06-30 23:02:48 +0300356.. method:: epoll.poll(timeout=None, maxevents=-1)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000357
358 Wait for events. timeout in seconds (float)
359
Victor Stinner41eba222015-03-30 21:59:21 +0200360 .. versionchanged:: 3.5
361 The function is now retried with a recomputed timeout when interrupted by
362 a signal, except if the signal handler raises an exception (see
363 :pep:`475` for the rationale), instead of raising
364 :exc:`InterruptedError`.
365
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000366
Georg Brandl116aa622007-08-15 14:28:22 +0000367.. _poll-objects:
368
369Polling Objects
370---------------
371
Georg Brandl60203b42010-10-06 10:11:56 +0000372The :c:func:`poll` system call, supported on most Unix systems, provides better
Georg Brandl116aa622007-08-15 14:28:22 +0000373scalability for network servers that service many, many clients at the same
Georg Brandl60203b42010-10-06 10:11:56 +0000374time. :c:func:`poll` scales better because the system call only requires listing
375the file descriptors of interest, while :c:func:`select` builds a bitmap, turns
Georg Brandl116aa622007-08-15 14:28:22 +0000376on bits for the fds of interest, and then afterward the whole bitmap has to be
Georg Brandl60203b42010-10-06 10:11:56 +0000377linearly scanned again. :c:func:`select` is O(highest file descriptor), while
378:c:func:`poll` is O(number of file descriptors).
Georg Brandl116aa622007-08-15 14:28:22 +0000379
380
381.. method:: poll.register(fd[, eventmask])
382
383 Register a file descriptor with the polling object. Future calls to the
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300384 :meth:`poll` method will then check whether the file descriptor has any
385 pending I/O events. *fd* can be either an integer, or an object with a
386 :meth:`~io.IOBase.fileno` method that returns an integer. File objects
387 implement :meth:`!fileno`, so they can also be used as the argument.
Georg Brandl116aa622007-08-15 14:28:22 +0000388
389 *eventmask* is an optional bitmask describing the type of events you want to
390 check for, and can be a combination of the constants :const:`POLLIN`,
391 :const:`POLLPRI`, and :const:`POLLOUT`, described in the table below. If not
392 specified, the default value used will check for all 3 types of events.
393
394 +-------------------+------------------------------------------+
395 | Constant | Meaning |
396 +===================+==========================================+
397 | :const:`POLLIN` | There is data to read |
398 +-------------------+------------------------------------------+
399 | :const:`POLLPRI` | There is urgent data to read |
400 +-------------------+------------------------------------------+
401 | :const:`POLLOUT` | Ready for output: writing will not block |
402 +-------------------+------------------------------------------+
403 | :const:`POLLERR` | Error condition of some sort |
404 +-------------------+------------------------------------------+
405 | :const:`POLLHUP` | Hung up |
406 +-------------------+------------------------------------------+
Berker Peksagfe8d9662016-07-19 21:09:26 +0300407 | :const:`POLLRDHUP`| Stream socket peer closed connection, or |
408 | | shut down writing half of connection |
409 +-------------------+------------------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +0000410 | :const:`POLLNVAL` | Invalid request: descriptor not open |
411 +-------------------+------------------------------------------+
412
413 Registering a file descriptor that's already registered is not an error, and has
414 the same effect as registering the descriptor exactly once.
415
416
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000417.. method:: poll.modify(fd, eventmask)
418
419 Modifies an already registered fd. This has the same effect as
Jean-Paul Calderone7f94f392010-07-18 16:13:27 +0000420 ``register(fd, eventmask)``. Attempting to modify a file descriptor
Andrew Svetlov050f9ea2014-04-01 00:23:23 +0300421 that was never registered causes an :exc:`OSError` exception with errno
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000422 :const:`ENOENT` to be raised.
423
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000424
Georg Brandl116aa622007-08-15 14:28:22 +0000425.. method:: poll.unregister(fd)
426
427 Remove a file descriptor being tracked by a polling object. Just like the
428 :meth:`register` method, *fd* can be an integer or an object with a
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300429 :meth:`~io.IOBase.fileno` method that returns an integer.
Georg Brandl116aa622007-08-15 14:28:22 +0000430
431 Attempting to remove a file descriptor that was never registered causes a
432 :exc:`KeyError` exception to be raised.
433
434
435.. method:: poll.poll([timeout])
436
437 Polls the set of registered file descriptors, and returns a possibly-empty list
438 containing ``(fd, event)`` 2-tuples for the descriptors that have events or
439 errors to report. *fd* is the file descriptor, and *event* is a bitmask with
440 bits set for the reported events for that descriptor --- :const:`POLLIN` for
441 waiting input, :const:`POLLOUT` to indicate that the descriptor can be written
442 to, and so forth. An empty list indicates that the call timed out and no file
443 descriptors had any events to report. If *timeout* is given, it specifies the
444 length of time in milliseconds which the system will wait for events before
445 returning. If *timeout* is omitted, negative, or :const:`None`, the call will
446 block until there is an event for this poll object.
447
Victor Stinner3c7d6e02015-03-30 21:38:00 +0200448 .. versionchanged:: 3.5
449 The function is now retried with a recomputed timeout when interrupted by
450 a signal, except if the signal handler raises an exception (see
451 :pep:`475` for the rationale), instead of raising
452 :exc:`InterruptedError`.
453
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000454
455.. _kqueue-objects:
456
457Kqueue Objects
458--------------
459
460.. method:: kqueue.close()
461
462 Close the control file descriptor of the kqueue object.
463
464
Victor Stinner13423c32013-08-22 00:19:50 +0200465.. attribute:: kqueue.closed
466
467 ``True`` if the kqueue object is closed.
468
469
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000470.. method:: kqueue.fileno()
471
472 Return the file descriptor number of the control fd.
473
474
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000475.. method:: kqueue.fromfd(fd)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000476
477 Create a kqueue object from a given file descriptor.
478
479
Hynek Schlawack979f37a2012-05-22 16:12:18 +0200480.. method:: kqueue.control(changelist, max_events[, timeout=None]) -> eventlist
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000481
482 Low level interface to kevent
483
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300484 - changelist must be an iterable of kevent object or ``None``
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000485 - max_events must be 0 or a positive integer
486 - timeout in seconds (floats possible)
487
Victor Stinner4448c082015-03-31 11:48:34 +0200488 .. versionchanged:: 3.5
489 The function is now retried with a recomputed timeout when interrupted by
490 a signal, except if the signal handler raises an exception (see
491 :pep:`475` for the rationale), instead of raising
492 :exc:`InterruptedError`.
493
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000494
495.. _kevent-objects:
496
497Kevent Objects
498--------------
499
Serhiy Storchaka6dff0202016-05-07 10:49:07 +0300500https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000501
Christian Heimesfe337bf2008-03-23 21:54:12 +0000502.. attribute:: kevent.ident
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000503
504 Value used to identify the event. The interpretation depends on the filter
505 but it's usually the file descriptor. In the constructor ident can either
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300506 be an int or an object with a :meth:`~io.IOBase.fileno` method. kevent
507 stores the integer internally.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000508
Christian Heimesfe337bf2008-03-23 21:54:12 +0000509.. attribute:: kevent.filter
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000510
Georg Brandl1b5ab452009-08-13 07:56:35 +0000511 Name of the kernel filter.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000512
513 +---------------------------+---------------------------------------------+
514 | Constant | Meaning |
515 +===========================+=============================================+
516 | :const:`KQ_FILTER_READ` | Takes a descriptor and returns whenever |
517 | | there is data available to read |
518 +---------------------------+---------------------------------------------+
519 | :const:`KQ_FILTER_WRITE` | Takes a descriptor and returns whenever |
Georg Brandl1b5ab452009-08-13 07:56:35 +0000520 | | there is data available to write |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000521 +---------------------------+---------------------------------------------+
522 | :const:`KQ_FILTER_AIO` | AIO requests |
523 +---------------------------+---------------------------------------------+
524 | :const:`KQ_FILTER_VNODE` | Returns when one or more of the requested |
525 | | events watched in *fflag* occurs |
526 +---------------------------+---------------------------------------------+
527 | :const:`KQ_FILTER_PROC` | Watch for events on a process id |
528 +---------------------------+---------------------------------------------+
529 | :const:`KQ_FILTER_NETDEV` | Watch for events on a network device |
530 | | [not available on Mac OS X] |
531 +---------------------------+---------------------------------------------+
532 | :const:`KQ_FILTER_SIGNAL` | Returns whenever the watched signal is |
533 | | delivered to the process |
534 +---------------------------+---------------------------------------------+
535 | :const:`KQ_FILTER_TIMER` | Establishes an arbitrary timer |
536 +---------------------------+---------------------------------------------+
537
Christian Heimesfe337bf2008-03-23 21:54:12 +0000538.. attribute:: kevent.flags
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000539
Georg Brandl1b5ab452009-08-13 07:56:35 +0000540 Filter action.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000541
542 +---------------------------+---------------------------------------------+
543 | Constant | Meaning |
544 +===========================+=============================================+
545 | :const:`KQ_EV_ADD` | Adds or modifies an event |
546 +---------------------------+---------------------------------------------+
547 | :const:`KQ_EV_DELETE` | Removes an event from the queue |
548 +---------------------------+---------------------------------------------+
549 | :const:`KQ_EV_ENABLE` | Permitscontrol() to returns the event |
550 +---------------------------+---------------------------------------------+
551 | :const:`KQ_EV_DISABLE` | Disablesevent |
552 +---------------------------+---------------------------------------------+
Georg Brandl2ee470f2008-07-16 12:55:28 +0000553 | :const:`KQ_EV_ONESHOT` | Removes event after first occurrence |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000554 +---------------------------+---------------------------------------------+
555 | :const:`KQ_EV_CLEAR` | Reset the state after an event is retrieved |
556 +---------------------------+---------------------------------------------+
557 | :const:`KQ_EV_SYSFLAGS` | internal event |
558 +---------------------------+---------------------------------------------+
559 | :const:`KQ_EV_FLAG1` | internal event |
560 +---------------------------+---------------------------------------------+
561 | :const:`KQ_EV_EOF` | Filter specific EOF condition |
562 +---------------------------+---------------------------------------------+
563 | :const:`KQ_EV_ERROR` | See return values |
564 +---------------------------+---------------------------------------------+
565
566
Christian Heimesfe337bf2008-03-23 21:54:12 +0000567.. attribute:: kevent.fflags
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000568
Georg Brandl1b5ab452009-08-13 07:56:35 +0000569 Filter specific flags.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000570
Georg Brandl1b5ab452009-08-13 07:56:35 +0000571 :const:`KQ_FILTER_READ` and :const:`KQ_FILTER_WRITE` filter flags:
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000572
573 +----------------------------+--------------------------------------------+
574 | Constant | Meaning |
575 +============================+============================================+
576 | :const:`KQ_NOTE_LOWAT` | low water mark of a socket buffer |
577 +----------------------------+--------------------------------------------+
578
Georg Brandl1b5ab452009-08-13 07:56:35 +0000579 :const:`KQ_FILTER_VNODE` filter flags:
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000580
581 +----------------------------+--------------------------------------------+
582 | Constant | Meaning |
583 +============================+============================================+
584 | :const:`KQ_NOTE_DELETE` | *unlink()* was called |
585 +----------------------------+--------------------------------------------+
Georg Brandl2ee470f2008-07-16 12:55:28 +0000586 | :const:`KQ_NOTE_WRITE` | a write occurred |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000587 +----------------------------+--------------------------------------------+
588 | :const:`KQ_NOTE_EXTEND` | the file was extended |
589 +----------------------------+--------------------------------------------+
590 | :const:`KQ_NOTE_ATTRIB` | an attribute was changed |
591 +----------------------------+--------------------------------------------+
592 | :const:`KQ_NOTE_LINK` | the link count has changed |
593 +----------------------------+--------------------------------------------+
594 | :const:`KQ_NOTE_RENAME` | the file was renamed |
595 +----------------------------+--------------------------------------------+
596 | :const:`KQ_NOTE_REVOKE` | access to the file was revoked |
597 +----------------------------+--------------------------------------------+
598
Georg Brandl1b5ab452009-08-13 07:56:35 +0000599 :const:`KQ_FILTER_PROC` filter flags:
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000600
601 +----------------------------+--------------------------------------------+
602 | Constant | Meaning |
603 +============================+============================================+
604 | :const:`KQ_NOTE_EXIT` | the process has exited |
605 +----------------------------+--------------------------------------------+
606 | :const:`KQ_NOTE_FORK` | the process has called *fork()* |
607 +----------------------------+--------------------------------------------+
608 | :const:`KQ_NOTE_EXEC` | the process has executed a new process |
609 +----------------------------+--------------------------------------------+
610 | :const:`KQ_NOTE_PCTRLMASK` | internal filter flag |
611 +----------------------------+--------------------------------------------+
612 | :const:`KQ_NOTE_PDATAMASK` | internal filter flag |
613 +----------------------------+--------------------------------------------+
614 | :const:`KQ_NOTE_TRACK` | follow a process across *fork()* |
615 +----------------------------+--------------------------------------------+
616 | :const:`KQ_NOTE_CHILD` | returned on the child process for |
617 | | *NOTE_TRACK* |
618 +----------------------------+--------------------------------------------+
619 | :const:`KQ_NOTE_TRACKERR` | unable to attach to a child |
620 +----------------------------+--------------------------------------------+
621
Georg Brandl1b5ab452009-08-13 07:56:35 +0000622 :const:`KQ_FILTER_NETDEV` filter flags (not available on Mac OS X):
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000623
624 +----------------------------+--------------------------------------------+
625 | Constant | Meaning |
626 +============================+============================================+
627 | :const:`KQ_NOTE_LINKUP` | link is up |
628 +----------------------------+--------------------------------------------+
629 | :const:`KQ_NOTE_LINKDOWN` | link is down |
630 +----------------------------+--------------------------------------------+
631 | :const:`KQ_NOTE_LINKINV` | link state is invalid |
632 +----------------------------+--------------------------------------------+
633
634
Christian Heimesfe337bf2008-03-23 21:54:12 +0000635.. attribute:: kevent.data
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000636
Georg Brandl1b5ab452009-08-13 07:56:35 +0000637 Filter specific data.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000638
639
Christian Heimesfe337bf2008-03-23 21:54:12 +0000640.. attribute:: kevent.udata
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000641
Georg Brandl1b5ab452009-08-13 07:56:35 +0000642 User defined value.