blob: 6cec9f764bf647c0408b608000e161e78ed646bf [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
60 events. *sizehint* is deprecated and completely ignored. *flags* can be set
61 to :const:`EPOLL_CLOEXEC`, which causes the epoll descriptor to be closed
R David Murray2bc930f2013-12-31 11:17:21 -050062 automatically when :func:`os.execve` is called.
63
64 See the :ref:`epoll-objects` section below for the methods supported by
65 epolling objects.
66
67 ``epoll`` objects support the context management protocol: when used in a
68 :keyword:`with` statement, the new file descriptor is automatically closed
69 at the end of the block.
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -060070
Victor Stinnerdaf45552013-08-28 00:53:59 +020071 The new file descriptor is :ref:`non-inheritable <fd_inheritance>`.
72
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -060073 .. versionchanged:: 3.3
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -060074 Added the *flags* parameter.
Christian Heimes4fbc72b2008-03-22 00:47:35 +000075
Antoine Pitrou09bb89b2012-12-15 21:14:21 +010076 .. versionchanged:: 3.4
77 Support for the :keyword:`with` statement was added.
Victor Stinnerdaf45552013-08-28 00:53:59 +020078 The new file descriptor is now non-inheritable.
Antoine Pitrou09bb89b2012-12-15 21:14:21 +010079
Christian Heimes4fbc72b2008-03-22 00:47:35 +000080
Georg Brandl116aa622007-08-15 14:28:22 +000081.. function:: poll()
82
83 (Not supported by all operating systems.) Returns a polling object, which
84 supports registering and unregistering file descriptors, and then polling them
85 for I/O events; see section :ref:`poll-objects` below for the methods supported
86 by polling objects.
87
88
Christian Heimesfe337bf2008-03-23 21:54:12 +000089.. function:: kqueue()
Christian Heimes4fbc72b2008-03-22 00:47:35 +000090
Georg Brandle767e042010-07-14 08:00:22 +000091 (Only supported on BSD.) Returns a kernel queue object; see section
Christian Heimesfe337bf2008-03-23 21:54:12 +000092 :ref:`kqueue-objects` below for the methods supported by kqueue objects.
Christian Heimes4fbc72b2008-03-22 00:47:35 +000093
Victor Stinnerdaf45552013-08-28 00:53:59 +020094 The new file descriptor is :ref:`non-inheritable <fd_inheritance>`.
95
96 .. versionchanged:: 3.4
97 The new file descriptor is now non-inheritable.
98
Christian Heimes4fbc72b2008-03-22 00:47:35 +000099
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000100.. function:: kevent(ident, filter=KQ_FILTER_READ, flags=KQ_EV_ADD, fflags=0, data=0, udata=0)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000101
Georg Brandle767e042010-07-14 08:00:22 +0000102 (Only supported on BSD.) Returns a kernel event object; see section
103 :ref:`kevent-objects` below for the methods supported by kevent objects.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000104
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000105
Georg Brandl734e2682008-08-12 08:18:18 +0000106.. function:: select(rlist, wlist, xlist[, timeout])
Georg Brandl116aa622007-08-15 14:28:22 +0000107
Georg Brandl60203b42010-10-06 10:11:56 +0000108 This is a straightforward interface to the Unix :c:func:`select` system call.
Georg Brandl116aa622007-08-15 14:28:22 +0000109 The first three arguments are sequences of 'waitable objects': either
110 integers representing file descriptors or objects with a parameterless method
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300111 named :meth:`~io.IOBase.fileno` returning such an integer:
Georg Brandl734e2682008-08-12 08:18:18 +0000112
113 * *rlist*: wait until ready for reading
114 * *wlist*: wait until ready for writing
115 * *xlist*: wait for an "exceptional condition" (see the manual page for what
116 your system considers such a condition)
117
118 Empty sequences are allowed, but acceptance of three empty sequences is
119 platform-dependent. (It is known to work on Unix but not on Windows.) The
120 optional *timeout* argument specifies a time-out as a floating point number
121 in seconds. When the *timeout* argument is omitted the function blocks until
122 at least one file descriptor is ready. A time-out value of zero specifies a
123 poll and never blocks.
Georg Brandl116aa622007-08-15 14:28:22 +0000124
125 The return value is a triple of lists of objects that are ready: subsets of the
126 first three arguments. When the time-out is reached without a file descriptor
127 becoming ready, three empty lists are returned.
128
129 .. index::
130 single: socket() (in module socket)
131 single: popen() (in module os)
132
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000133 Among the acceptable object types in the sequences are Python :term:`file
134 objects <file object>` (e.g. ``sys.stdin``, or objects returned by
135 :func:`open` or :func:`os.popen`), socket objects returned by
136 :func:`socket.socket`. You may also define a :dfn:`wrapper` class yourself,
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300137 as long as it has an appropriate :meth:`~io.IOBase.fileno` method (that
138 really returns a file descriptor, not just a random integer).
Georg Brandl116aa622007-08-15 14:28:22 +0000139
Georg Brandl116aa622007-08-15 14:28:22 +0000140 .. note::
141
142 .. index:: single: WinSock
143
Georg Brandl734e2682008-08-12 08:18:18 +0000144 File objects on Windows are not acceptable, but sockets are. On Windows,
Georg Brandl60203b42010-10-06 10:11:56 +0000145 the underlying :c:func:`select` function is provided by the WinSock
Georg Brandl734e2682008-08-12 08:18:18 +0000146 library, and does not handle file descriptors that don't originate from
147 WinSock.
Georg Brandl116aa622007-08-15 14:28:22 +0000148
Victor Stinnerf70e1ca2015-03-30 21:16:11 +0200149 .. versionchanged:: 3.5
150 The function is now retried with a recomputed timeout when interrupted by
151 a signal, except if the signal handler raises an exception (see
152 :pep:`475` for the rationale), instead of raising
153 :exc:`InterruptedError`.
154
155
Antoine Pitroucfad97b2011-01-25 17:24:57 +0000156.. attribute:: PIPE_BUF
Gregory P. Smithb970b862009-07-04 02:28:47 +0000157
Antoine Pitrouda7be3e2011-01-25 16:28:44 +0000158 The minimum number of bytes which can be written without blocking to a pipe
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300159 when the pipe has been reported as ready for writing by :func:`~select.select`,
Antoine Pitrouda7be3e2011-01-25 16:28:44 +0000160 :func:`poll` or another interface in this module. This doesn't apply
161 to other kind of file-like objects such as sockets.
162
Amaury Forgeot d'Arcace31022009-07-09 22:44:11 +0000163 This value is guaranteed by POSIX to be at least 512. Availability: Unix.
Gregory P. Smithb970b862009-07-04 02:28:47 +0000164
Mark Dickinson574b1d62009-10-01 20:20:09 +0000165 .. versionadded:: 3.2
Gregory P. Smithb970b862009-07-04 02:28:47 +0000166
Georg Brandl116aa622007-08-15 14:28:22 +0000167
Jesus Cead8b9ae62011-11-14 19:07:41 +0100168.. _devpoll-objects:
169
170``/dev/poll`` Polling Objects
Georg Brandl525d3552014-10-29 10:26:56 +0100171-----------------------------
Jesus Cead8b9ae62011-11-14 19:07:41 +0100172
173Solaris and derivatives have ``/dev/poll``. While :c:func:`select` is
174O(highest file descriptor) and :c:func:`poll` is O(number of file
175descriptors), ``/dev/poll`` is O(active file descriptors).
176
177``/dev/poll`` behaviour is very close to the standard :c:func:`poll`
178object.
179
180
Victor Stinner13423c32013-08-22 00:19:50 +0200181.. method:: devpoll.close()
182
183 Close the file descriptor of the polling object.
184
185 .. versionadded:: 3.4
186
187
188.. attribute:: devpoll.closed
189
190 ``True`` if the polling object is closed.
191
192 .. versionadded:: 3.4
193
194
195.. method:: devpoll.fileno()
196
197 Return the file descriptor number of the polling object.
198
199 .. versionadded:: 3.4
200
201
Jesus Cead8b9ae62011-11-14 19:07:41 +0100202.. method:: devpoll.register(fd[, eventmask])
203
204 Register a file descriptor with the polling object. Future calls to the
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300205 :meth:`poll` method will then check whether the file descriptor has any
206 pending I/O events. *fd* can be either an integer, or an object with a
207 :meth:`~io.IOBase.fileno` method that returns an integer. File objects
208 implement :meth:`!fileno`, so they can also be used as the argument.
Jesus Cead8b9ae62011-11-14 19:07:41 +0100209
210 *eventmask* is an optional bitmask describing the type of events you want to
211 check for. The constants are the same that with :c:func:`poll`
212 object. The default value is a combination of the constants :const:`POLLIN`,
213 :const:`POLLPRI`, and :const:`POLLOUT`.
214
215 .. warning::
216
217 Registering a file descriptor that's already registered is not an
Donald Stufft8b852f12014-05-20 12:58:38 -0400218 error, but the result is undefined. The appropriate action is to
Jesus Cead8b9ae62011-11-14 19:07:41 +0100219 unregister or modify it first. This is an important difference
220 compared with :c:func:`poll`.
221
222
223.. method:: devpoll.modify(fd[, eventmask])
224
225 This method does an :meth:`unregister` followed by a
226 :meth:`register`. It is (a bit) more efficient that doing the same
227 explicitly.
228
229
230.. method:: devpoll.unregister(fd)
231
232 Remove a file descriptor being tracked by a polling object. Just like the
233 :meth:`register` method, *fd* can be an integer or an object with a
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300234 :meth:`~io.IOBase.fileno` method that returns an integer.
Jesus Cead8b9ae62011-11-14 19:07:41 +0100235
236 Attempting to remove a file descriptor that was never registered is
237 safely ignored.
238
239
240.. method:: devpoll.poll([timeout])
241
242 Polls the set of registered file descriptors, and returns a possibly-empty list
243 containing ``(fd, event)`` 2-tuples for the descriptors that have events or
244 errors to report. *fd* is the file descriptor, and *event* is a bitmask with
245 bits set for the reported events for that descriptor --- :const:`POLLIN` for
246 waiting input, :const:`POLLOUT` to indicate that the descriptor can be written
247 to, and so forth. An empty list indicates that the call timed out and no file
248 descriptors had any events to report. If *timeout* is given, it specifies the
249 length of time in milliseconds which the system will wait for events before
250 returning. If *timeout* is omitted, -1, or :const:`None`, the call will
251 block until there is an event for this poll object.
252
Victor Stinner45ca48b2015-03-31 12:10:33 +0200253 .. versionchanged:: 3.5
254 The function is now retried with a recomputed timeout when interrupted by
255 a signal, except if the signal handler raises an exception (see
256 :pep:`475` for the rationale), instead of raising
257 :exc:`InterruptedError`.
258
Jesus Cead8b9ae62011-11-14 19:07:41 +0100259
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000260.. _epoll-objects:
261
262Edge and Level Trigger Polling (epoll) Objects
263----------------------------------------------
264
265 http://linux.die.net/man/4/epoll
266
267 *eventmask*
268
269 +-----------------------+-----------------------------------------------+
270 | Constant | Meaning |
271 +=======================+===============================================+
272 | :const:`EPOLLIN` | Available for read |
273 +-----------------------+-----------------------------------------------+
274 | :const:`EPOLLOUT` | Available for write |
275 +-----------------------+-----------------------------------------------+
276 | :const:`EPOLLPRI` | Urgent data for read |
277 +-----------------------+-----------------------------------------------+
Christian Heimes5e696852008-04-09 08:37:03 +0000278 | :const:`EPOLLERR` | Error condition happened on the assoc. fd |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000279 +-----------------------+-----------------------------------------------+
Christian Heimes5e696852008-04-09 08:37:03 +0000280 | :const:`EPOLLHUP` | Hang up happened on the assoc. fd |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000281 +-----------------------+-----------------------------------------------+
282 | :const:`EPOLLET` | Set Edge Trigger behavior, the default is |
283 | | Level Trigger behavior |
284 +-----------------------+-----------------------------------------------+
285 | :const:`EPOLLONESHOT` | Set one-shot behavior. After one event is |
286 | | pulled out, the fd is internally disabled |
287 +-----------------------+-----------------------------------------------+
Jean-Paul Calderone7f54dce2010-07-18 16:30:31 +0000288 | :const:`EPOLLRDNORM` | Equivalent to :const:`EPOLLIN` |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000289 +-----------------------+-----------------------------------------------+
Jean-Paul Calderone7f54dce2010-07-18 16:30:31 +0000290 | :const:`EPOLLRDBAND` | Priority data band can be read. |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000291 +-----------------------+-----------------------------------------------+
Jean-Paul Calderone7f54dce2010-07-18 16:30:31 +0000292 | :const:`EPOLLWRNORM` | Equivalent to :const:`EPOLLOUT` |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000293 +-----------------------+-----------------------------------------------+
Jean-Paul Calderone7f54dce2010-07-18 16:30:31 +0000294 | :const:`EPOLLWRBAND` | Priority data may be written. |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000295 +-----------------------+-----------------------------------------------+
Jean-Paul Calderone7f54dce2010-07-18 16:30:31 +0000296 | :const:`EPOLLMSG` | Ignored. |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000297 +-----------------------+-----------------------------------------------+
298
299
300.. method:: epoll.close()
301
302 Close the control file descriptor of the epoll object.
303
304
Victor Stinner13423c32013-08-22 00:19:50 +0200305.. attribute:: epoll.closed
306
307 ``True`` if the epoll object is closed.
308
309
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000310.. method:: epoll.fileno()
311
312 Return the file descriptor number of the control fd.
313
314
315.. method:: epoll.fromfd(fd)
316
317 Create an epoll object from a given file descriptor.
318
319
320.. method:: epoll.register(fd[, eventmask])
321
322 Register a fd descriptor with the epoll object.
323
324
325.. method:: epoll.modify(fd, eventmask)
326
Georg Brandl632c8122014-01-12 18:03:12 +0100327 Modify a registered file descriptor.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000328
329
330.. method:: epoll.unregister(fd)
331
332 Remove a registered file descriptor from the epoll object.
333
334
Hynek Schlawackdfa46522012-05-21 11:01:54 +0200335.. method:: epoll.poll(timeout=-1, maxevents=-1)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000336
337 Wait for events. timeout in seconds (float)
338
Victor Stinner41eba222015-03-30 21:59:21 +0200339 .. versionchanged:: 3.5
340 The function is now retried with a recomputed timeout when interrupted by
341 a signal, except if the signal handler raises an exception (see
342 :pep:`475` for the rationale), instead of raising
343 :exc:`InterruptedError`.
344
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000345
Georg Brandl116aa622007-08-15 14:28:22 +0000346.. _poll-objects:
347
348Polling Objects
349---------------
350
Georg Brandl60203b42010-10-06 10:11:56 +0000351The :c:func:`poll` system call, supported on most Unix systems, provides better
Georg Brandl116aa622007-08-15 14:28:22 +0000352scalability for network servers that service many, many clients at the same
Georg Brandl60203b42010-10-06 10:11:56 +0000353time. :c:func:`poll` scales better because the system call only requires listing
354the file descriptors of interest, while :c:func:`select` builds a bitmap, turns
Georg Brandl116aa622007-08-15 14:28:22 +0000355on bits for the fds of interest, and then afterward the whole bitmap has to be
Georg Brandl60203b42010-10-06 10:11:56 +0000356linearly scanned again. :c:func:`select` is O(highest file descriptor), while
357:c:func:`poll` is O(number of file descriptors).
Georg Brandl116aa622007-08-15 14:28:22 +0000358
359
360.. method:: poll.register(fd[, eventmask])
361
362 Register a file descriptor with the polling object. Future calls to the
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300363 :meth:`poll` method will then check whether the file descriptor has any
364 pending I/O events. *fd* can be either an integer, or an object with a
365 :meth:`~io.IOBase.fileno` method that returns an integer. File objects
366 implement :meth:`!fileno`, so they can also be used as the argument.
Georg Brandl116aa622007-08-15 14:28:22 +0000367
368 *eventmask* is an optional bitmask describing the type of events you want to
369 check for, and can be a combination of the constants :const:`POLLIN`,
370 :const:`POLLPRI`, and :const:`POLLOUT`, described in the table below. If not
371 specified, the default value used will check for all 3 types of events.
372
373 +-------------------+------------------------------------------+
374 | Constant | Meaning |
375 +===================+==========================================+
376 | :const:`POLLIN` | There is data to read |
377 +-------------------+------------------------------------------+
378 | :const:`POLLPRI` | There is urgent data to read |
379 +-------------------+------------------------------------------+
380 | :const:`POLLOUT` | Ready for output: writing will not block |
381 +-------------------+------------------------------------------+
382 | :const:`POLLERR` | Error condition of some sort |
383 +-------------------+------------------------------------------+
384 | :const:`POLLHUP` | Hung up |
385 +-------------------+------------------------------------------+
386 | :const:`POLLNVAL` | Invalid request: descriptor not open |
387 +-------------------+------------------------------------------+
388
389 Registering a file descriptor that's already registered is not an error, and has
390 the same effect as registering the descriptor exactly once.
391
392
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000393.. method:: poll.modify(fd, eventmask)
394
395 Modifies an already registered fd. This has the same effect as
Jean-Paul Calderone7f94f392010-07-18 16:13:27 +0000396 ``register(fd, eventmask)``. Attempting to modify a file descriptor
Andrew Svetlov050f9ea2014-04-01 00:23:23 +0300397 that was never registered causes an :exc:`OSError` exception with errno
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000398 :const:`ENOENT` to be raised.
399
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000400
Georg Brandl116aa622007-08-15 14:28:22 +0000401.. method:: poll.unregister(fd)
402
403 Remove a file descriptor being tracked by a polling object. Just like the
404 :meth:`register` method, *fd* can be an integer or an object with a
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300405 :meth:`~io.IOBase.fileno` method that returns an integer.
Georg Brandl116aa622007-08-15 14:28:22 +0000406
407 Attempting to remove a file descriptor that was never registered causes a
408 :exc:`KeyError` exception to be raised.
409
410
411.. method:: poll.poll([timeout])
412
413 Polls the set of registered file descriptors, and returns a possibly-empty list
414 containing ``(fd, event)`` 2-tuples for the descriptors that have events or
415 errors to report. *fd* is the file descriptor, and *event* is a bitmask with
416 bits set for the reported events for that descriptor --- :const:`POLLIN` for
417 waiting input, :const:`POLLOUT` to indicate that the descriptor can be written
418 to, and so forth. An empty list indicates that the call timed out and no file
419 descriptors had any events to report. If *timeout* is given, it specifies the
420 length of time in milliseconds which the system will wait for events before
421 returning. If *timeout* is omitted, negative, or :const:`None`, the call will
422 block until there is an event for this poll object.
423
Victor Stinner3c7d6e02015-03-30 21:38:00 +0200424 .. versionchanged:: 3.5
425 The function is now retried with a recomputed timeout when interrupted by
426 a signal, except if the signal handler raises an exception (see
427 :pep:`475` for the rationale), instead of raising
428 :exc:`InterruptedError`.
429
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000430
431.. _kqueue-objects:
432
433Kqueue Objects
434--------------
435
436.. method:: kqueue.close()
437
438 Close the control file descriptor of the kqueue object.
439
440
Victor Stinner13423c32013-08-22 00:19:50 +0200441.. attribute:: kqueue.closed
442
443 ``True`` if the kqueue object is closed.
444
445
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000446.. method:: kqueue.fileno()
447
448 Return the file descriptor number of the control fd.
449
450
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000451.. method:: kqueue.fromfd(fd)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000452
453 Create a kqueue object from a given file descriptor.
454
455
Hynek Schlawack979f37a2012-05-22 16:12:18 +0200456.. method:: kqueue.control(changelist, max_events[, timeout=None]) -> eventlist
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000457
458 Low level interface to kevent
459
460 - changelist must be an iterable of kevent object or None
461 - max_events must be 0 or a positive integer
462 - timeout in seconds (floats possible)
463
Victor Stinner4448c082015-03-31 11:48:34 +0200464 .. versionchanged:: 3.5
465 The function is now retried with a recomputed timeout when interrupted by
466 a signal, except if the signal handler raises an exception (see
467 :pep:`475` for the rationale), instead of raising
468 :exc:`InterruptedError`.
469
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000470
471.. _kevent-objects:
472
473Kevent Objects
474--------------
475
Serhiy Storchaka6dff0202016-05-07 10:49:07 +0300476https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000477
Christian Heimesfe337bf2008-03-23 21:54:12 +0000478.. attribute:: kevent.ident
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000479
480 Value used to identify the event. The interpretation depends on the filter
481 but it's usually the file descriptor. In the constructor ident can either
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300482 be an int or an object with a :meth:`~io.IOBase.fileno` method. kevent
483 stores the integer internally.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000484
Christian Heimesfe337bf2008-03-23 21:54:12 +0000485.. attribute:: kevent.filter
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000486
Georg Brandl1b5ab452009-08-13 07:56:35 +0000487 Name of the kernel filter.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000488
489 +---------------------------+---------------------------------------------+
490 | Constant | Meaning |
491 +===========================+=============================================+
492 | :const:`KQ_FILTER_READ` | Takes a descriptor and returns whenever |
493 | | there is data available to read |
494 +---------------------------+---------------------------------------------+
495 | :const:`KQ_FILTER_WRITE` | Takes a descriptor and returns whenever |
Georg Brandl1b5ab452009-08-13 07:56:35 +0000496 | | there is data available to write |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000497 +---------------------------+---------------------------------------------+
498 | :const:`KQ_FILTER_AIO` | AIO requests |
499 +---------------------------+---------------------------------------------+
500 | :const:`KQ_FILTER_VNODE` | Returns when one or more of the requested |
501 | | events watched in *fflag* occurs |
502 +---------------------------+---------------------------------------------+
503 | :const:`KQ_FILTER_PROC` | Watch for events on a process id |
504 +---------------------------+---------------------------------------------+
505 | :const:`KQ_FILTER_NETDEV` | Watch for events on a network device |
506 | | [not available on Mac OS X] |
507 +---------------------------+---------------------------------------------+
508 | :const:`KQ_FILTER_SIGNAL` | Returns whenever the watched signal is |
509 | | delivered to the process |
510 +---------------------------+---------------------------------------------+
511 | :const:`KQ_FILTER_TIMER` | Establishes an arbitrary timer |
512 +---------------------------+---------------------------------------------+
513
Christian Heimesfe337bf2008-03-23 21:54:12 +0000514.. attribute:: kevent.flags
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000515
Georg Brandl1b5ab452009-08-13 07:56:35 +0000516 Filter action.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000517
518 +---------------------------+---------------------------------------------+
519 | Constant | Meaning |
520 +===========================+=============================================+
521 | :const:`KQ_EV_ADD` | Adds or modifies an event |
522 +---------------------------+---------------------------------------------+
523 | :const:`KQ_EV_DELETE` | Removes an event from the queue |
524 +---------------------------+---------------------------------------------+
525 | :const:`KQ_EV_ENABLE` | Permitscontrol() to returns the event |
526 +---------------------------+---------------------------------------------+
527 | :const:`KQ_EV_DISABLE` | Disablesevent |
528 +---------------------------+---------------------------------------------+
Georg Brandl2ee470f2008-07-16 12:55:28 +0000529 | :const:`KQ_EV_ONESHOT` | Removes event after first occurrence |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000530 +---------------------------+---------------------------------------------+
531 | :const:`KQ_EV_CLEAR` | Reset the state after an event is retrieved |
532 +---------------------------+---------------------------------------------+
533 | :const:`KQ_EV_SYSFLAGS` | internal event |
534 +---------------------------+---------------------------------------------+
535 | :const:`KQ_EV_FLAG1` | internal event |
536 +---------------------------+---------------------------------------------+
537 | :const:`KQ_EV_EOF` | Filter specific EOF condition |
538 +---------------------------+---------------------------------------------+
539 | :const:`KQ_EV_ERROR` | See return values |
540 +---------------------------+---------------------------------------------+
541
542
Christian Heimesfe337bf2008-03-23 21:54:12 +0000543.. attribute:: kevent.fflags
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000544
Georg Brandl1b5ab452009-08-13 07:56:35 +0000545 Filter specific flags.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000546
Georg Brandl1b5ab452009-08-13 07:56:35 +0000547 :const:`KQ_FILTER_READ` and :const:`KQ_FILTER_WRITE` filter flags:
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000548
549 +----------------------------+--------------------------------------------+
550 | Constant | Meaning |
551 +============================+============================================+
552 | :const:`KQ_NOTE_LOWAT` | low water mark of a socket buffer |
553 +----------------------------+--------------------------------------------+
554
Georg Brandl1b5ab452009-08-13 07:56:35 +0000555 :const:`KQ_FILTER_VNODE` filter flags:
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000556
557 +----------------------------+--------------------------------------------+
558 | Constant | Meaning |
559 +============================+============================================+
560 | :const:`KQ_NOTE_DELETE` | *unlink()* was called |
561 +----------------------------+--------------------------------------------+
Georg Brandl2ee470f2008-07-16 12:55:28 +0000562 | :const:`KQ_NOTE_WRITE` | a write occurred |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000563 +----------------------------+--------------------------------------------+
564 | :const:`KQ_NOTE_EXTEND` | the file was extended |
565 +----------------------------+--------------------------------------------+
566 | :const:`KQ_NOTE_ATTRIB` | an attribute was changed |
567 +----------------------------+--------------------------------------------+
568 | :const:`KQ_NOTE_LINK` | the link count has changed |
569 +----------------------------+--------------------------------------------+
570 | :const:`KQ_NOTE_RENAME` | the file was renamed |
571 +----------------------------+--------------------------------------------+
572 | :const:`KQ_NOTE_REVOKE` | access to the file was revoked |
573 +----------------------------+--------------------------------------------+
574
Georg Brandl1b5ab452009-08-13 07:56:35 +0000575 :const:`KQ_FILTER_PROC` filter flags:
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000576
577 +----------------------------+--------------------------------------------+
578 | Constant | Meaning |
579 +============================+============================================+
580 | :const:`KQ_NOTE_EXIT` | the process has exited |
581 +----------------------------+--------------------------------------------+
582 | :const:`KQ_NOTE_FORK` | the process has called *fork()* |
583 +----------------------------+--------------------------------------------+
584 | :const:`KQ_NOTE_EXEC` | the process has executed a new process |
585 +----------------------------+--------------------------------------------+
586 | :const:`KQ_NOTE_PCTRLMASK` | internal filter flag |
587 +----------------------------+--------------------------------------------+
588 | :const:`KQ_NOTE_PDATAMASK` | internal filter flag |
589 +----------------------------+--------------------------------------------+
590 | :const:`KQ_NOTE_TRACK` | follow a process across *fork()* |
591 +----------------------------+--------------------------------------------+
592 | :const:`KQ_NOTE_CHILD` | returned on the child process for |
593 | | *NOTE_TRACK* |
594 +----------------------------+--------------------------------------------+
595 | :const:`KQ_NOTE_TRACKERR` | unable to attach to a child |
596 +----------------------------+--------------------------------------------+
597
Georg Brandl1b5ab452009-08-13 07:56:35 +0000598 :const:`KQ_FILTER_NETDEV` filter flags (not available on Mac OS X):
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000599
600 +----------------------------+--------------------------------------------+
601 | Constant | Meaning |
602 +============================+============================================+
603 | :const:`KQ_NOTE_LINKUP` | link is up |
604 +----------------------------+--------------------------------------------+
605 | :const:`KQ_NOTE_LINKDOWN` | link is down |
606 +----------------------------+--------------------------------------------+
607 | :const:`KQ_NOTE_LINKINV` | link state is invalid |
608 +----------------------------+--------------------------------------------+
609
610
Christian Heimesfe337bf2008-03-23 21:54:12 +0000611.. attribute:: kevent.data
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000612
Georg Brandl1b5ab452009-08-13 07:56:35 +0000613 Filter specific data.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000614
615
Christian Heimesfe337bf2008-03-23 21:54:12 +0000616.. attribute:: kevent.udata
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000617
Georg Brandl1b5ab452009-08-13 07:56:35 +0000618 User defined value.