blob: e252e7adb9246ac05b99c5ee234a66cb222011ea [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
Berker Peksag1c697a52016-09-11 15:45:32 +030060 events. *sizehint* and *flags* are deprecated and completely ignored.
R David Murray2bc930f2013-12-31 11:17:21 -050061
62 See the :ref:`epoll-objects` section below for the methods supported by
63 epolling objects.
64
65 ``epoll`` objects support the context management protocol: when used in a
66 :keyword:`with` statement, the new file descriptor is automatically closed
67 at the end of the block.
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -060068
Victor Stinnerdaf45552013-08-28 00:53:59 +020069 The new file descriptor is :ref:`non-inheritable <fd_inheritance>`.
70
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -060071 .. versionchanged:: 3.3
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -060072 Added the *flags* parameter.
Christian Heimes4fbc72b2008-03-22 00:47:35 +000073
Antoine Pitrou09bb89b2012-12-15 21:14:21 +010074 .. versionchanged:: 3.4
75 Support for the :keyword:`with` statement was added.
Victor Stinnerdaf45552013-08-28 00:53:59 +020076 The new file descriptor is now non-inheritable.
Antoine Pitrou09bb89b2012-12-15 21:14:21 +010077
Berker Peksag1c697a52016-09-11 15:45:32 +030078 .. deprecated:: 3.4
79 The *flags* parameter. ``select.EPOLL_CLOEXEC`` is used by default now.
80 Use :func:`os.set_inheritable` to make the file descriptor inheritable.
81
Christian Heimes4fbc72b2008-03-22 00:47:35 +000082
Georg Brandl116aa622007-08-15 14:28:22 +000083.. function:: poll()
84
85 (Not supported by all operating systems.) Returns a polling object, which
86 supports registering and unregistering file descriptors, and then polling them
87 for I/O events; see section :ref:`poll-objects` below for the methods supported
88 by polling objects.
89
90
Christian Heimesfe337bf2008-03-23 21:54:12 +000091.. function:: kqueue()
Christian Heimes4fbc72b2008-03-22 00:47:35 +000092
Georg Brandle767e042010-07-14 08:00:22 +000093 (Only supported on BSD.) Returns a kernel queue object; see section
Christian Heimesfe337bf2008-03-23 21:54:12 +000094 :ref:`kqueue-objects` below for the methods supported by kqueue objects.
Christian Heimes4fbc72b2008-03-22 00:47:35 +000095
Victor Stinnerdaf45552013-08-28 00:53:59 +020096 The new file descriptor is :ref:`non-inheritable <fd_inheritance>`.
97
98 .. versionchanged:: 3.4
99 The new file descriptor is now non-inheritable.
100
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000101
Benjamin Peterson1baf4652009-12-31 03:11:23 +0000102.. function:: kevent(ident, filter=KQ_FILTER_READ, flags=KQ_EV_ADD, fflags=0, data=0, udata=0)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000103
Georg Brandle767e042010-07-14 08:00:22 +0000104 (Only supported on BSD.) Returns a kernel event object; see section
105 :ref:`kevent-objects` below for the methods supported by kevent objects.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000106
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000107
Georg Brandl734e2682008-08-12 08:18:18 +0000108.. function:: select(rlist, wlist, xlist[, timeout])
Georg Brandl116aa622007-08-15 14:28:22 +0000109
Georg Brandl60203b42010-10-06 10:11:56 +0000110 This is a straightforward interface to the Unix :c:func:`select` system call.
Georg Brandl116aa622007-08-15 14:28:22 +0000111 The first three arguments are sequences of 'waitable objects': either
112 integers representing file descriptors or objects with a parameterless method
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300113 named :meth:`~io.IOBase.fileno` returning such an integer:
Georg Brandl734e2682008-08-12 08:18:18 +0000114
115 * *rlist*: wait until ready for reading
116 * *wlist*: wait until ready for writing
117 * *xlist*: wait for an "exceptional condition" (see the manual page for what
118 your system considers such a condition)
119
120 Empty sequences are allowed, but acceptance of three empty sequences is
121 platform-dependent. (It is known to work on Unix but not on Windows.) The
122 optional *timeout* argument specifies a time-out as a floating point number
123 in seconds. When the *timeout* argument is omitted the function blocks until
124 at least one file descriptor is ready. A time-out value of zero specifies a
125 poll and never blocks.
Georg Brandl116aa622007-08-15 14:28:22 +0000126
127 The return value is a triple of lists of objects that are ready: subsets of the
128 first three arguments. When the time-out is reached without a file descriptor
129 becoming ready, three empty lists are returned.
130
131 .. index::
132 single: socket() (in module socket)
133 single: popen() (in module os)
134
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000135 Among the acceptable object types in the sequences are Python :term:`file
136 objects <file object>` (e.g. ``sys.stdin``, or objects returned by
137 :func:`open` or :func:`os.popen`), socket objects returned by
138 :func:`socket.socket`. You may also define a :dfn:`wrapper` class yourself,
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300139 as long as it has an appropriate :meth:`~io.IOBase.fileno` method (that
140 really returns a file descriptor, not just a random integer).
Georg Brandl116aa622007-08-15 14:28:22 +0000141
Georg Brandl116aa622007-08-15 14:28:22 +0000142 .. note::
143
144 .. index:: single: WinSock
145
Georg Brandl734e2682008-08-12 08:18:18 +0000146 File objects on Windows are not acceptable, but sockets are. On Windows,
Georg Brandl60203b42010-10-06 10:11:56 +0000147 the underlying :c:func:`select` function is provided by the WinSock
Georg Brandl734e2682008-08-12 08:18:18 +0000148 library, and does not handle file descriptors that don't originate from
149 WinSock.
Georg Brandl116aa622007-08-15 14:28:22 +0000150
Victor Stinnerf70e1ca2015-03-30 21:16:11 +0200151 .. versionchanged:: 3.5
152 The function is now retried with a recomputed timeout when interrupted by
153 a signal, except if the signal handler raises an exception (see
154 :pep:`475` for the rationale), instead of raising
155 :exc:`InterruptedError`.
156
157
Antoine Pitroucfad97b2011-01-25 17:24:57 +0000158.. attribute:: PIPE_BUF
Gregory P. Smithb970b862009-07-04 02:28:47 +0000159
Antoine Pitrouda7be3e2011-01-25 16:28:44 +0000160 The minimum number of bytes which can be written without blocking to a pipe
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300161 when the pipe has been reported as ready for writing by :func:`~select.select`,
Antoine Pitrouda7be3e2011-01-25 16:28:44 +0000162 :func:`poll` or another interface in this module. This doesn't apply
163 to other kind of file-like objects such as sockets.
164
Amaury Forgeot d'Arcace31022009-07-09 22:44:11 +0000165 This value is guaranteed by POSIX to be at least 512. Availability: Unix.
Gregory P. Smithb970b862009-07-04 02:28:47 +0000166
Mark Dickinson574b1d62009-10-01 20:20:09 +0000167 .. versionadded:: 3.2
Gregory P. Smithb970b862009-07-04 02:28:47 +0000168
Georg Brandl116aa622007-08-15 14:28:22 +0000169
Jesus Cead8b9ae62011-11-14 19:07:41 +0100170.. _devpoll-objects:
171
172``/dev/poll`` Polling Objects
Georg Brandl525d3552014-10-29 10:26:56 +0100173-----------------------------
Jesus Cead8b9ae62011-11-14 19:07:41 +0100174
175Solaris and derivatives have ``/dev/poll``. While :c:func:`select` is
176O(highest file descriptor) and :c:func:`poll` is O(number of file
177descriptors), ``/dev/poll`` is O(active file descriptors).
178
179``/dev/poll`` behaviour is very close to the standard :c:func:`poll`
180object.
181
182
Victor Stinner13423c32013-08-22 00:19:50 +0200183.. method:: devpoll.close()
184
185 Close the file descriptor of the polling object.
186
187 .. versionadded:: 3.4
188
189
190.. attribute:: devpoll.closed
191
192 ``True`` if the polling object is closed.
193
194 .. versionadded:: 3.4
195
196
197.. method:: devpoll.fileno()
198
199 Return the file descriptor number of the polling object.
200
201 .. versionadded:: 3.4
202
203
Jesus Cead8b9ae62011-11-14 19:07:41 +0100204.. method:: devpoll.register(fd[, eventmask])
205
206 Register a file descriptor with the polling object. Future calls to the
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300207 :meth:`poll` method will then check whether the file descriptor has any
208 pending I/O events. *fd* can be either an integer, or an object with a
209 :meth:`~io.IOBase.fileno` method that returns an integer. File objects
210 implement :meth:`!fileno`, so they can also be used as the argument.
Jesus Cead8b9ae62011-11-14 19:07:41 +0100211
212 *eventmask* is an optional bitmask describing the type of events you want to
213 check for. The constants are the same that with :c:func:`poll`
214 object. The default value is a combination of the constants :const:`POLLIN`,
215 :const:`POLLPRI`, and :const:`POLLOUT`.
216
217 .. warning::
218
219 Registering a file descriptor that's already registered is not an
Donald Stufft8b852f12014-05-20 12:58:38 -0400220 error, but the result is undefined. The appropriate action is to
Jesus Cead8b9ae62011-11-14 19:07:41 +0100221 unregister or modify it first. This is an important difference
222 compared with :c:func:`poll`.
223
224
225.. method:: devpoll.modify(fd[, eventmask])
226
227 This method does an :meth:`unregister` followed by a
228 :meth:`register`. It is (a bit) more efficient that doing the same
229 explicitly.
230
231
232.. method:: devpoll.unregister(fd)
233
234 Remove a file descriptor being tracked by a polling object. Just like the
235 :meth:`register` method, *fd* can be an integer or an object with a
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300236 :meth:`~io.IOBase.fileno` method that returns an integer.
Jesus Cead8b9ae62011-11-14 19:07:41 +0100237
238 Attempting to remove a file descriptor that was never registered is
239 safely ignored.
240
241
242.. method:: devpoll.poll([timeout])
243
244 Polls the set of registered file descriptors, and returns a possibly-empty list
245 containing ``(fd, event)`` 2-tuples for the descriptors that have events or
246 errors to report. *fd* is the file descriptor, and *event* is a bitmask with
247 bits set for the reported events for that descriptor --- :const:`POLLIN` for
248 waiting input, :const:`POLLOUT` to indicate that the descriptor can be written
249 to, and so forth. An empty list indicates that the call timed out and no file
250 descriptors had any events to report. If *timeout* is given, it specifies the
251 length of time in milliseconds which the system will wait for events before
252 returning. If *timeout* is omitted, -1, or :const:`None`, the call will
253 block until there is an event for this poll object.
254
Victor Stinner45ca48b2015-03-31 12:10:33 +0200255 .. versionchanged:: 3.5
256 The function is now retried with a recomputed timeout when interrupted by
257 a signal, except if the signal handler raises an exception (see
258 :pep:`475` for the rationale), instead of raising
259 :exc:`InterruptedError`.
260
Jesus Cead8b9ae62011-11-14 19:07:41 +0100261
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000262.. _epoll-objects:
263
264Edge and Level Trigger Polling (epoll) Objects
265----------------------------------------------
266
Sanyam Khurana1b4587a2017-12-06 22:09:33 +0530267 https://linux.die.net/man/4/epoll
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000268
269 *eventmask*
270
Benjamin Peterson0715ce32016-07-18 22:02:44 -0700271 +-------------------------+-----------------------------------------------+
272 | Constant | Meaning |
273 +=========================+===============================================+
274 | :const:`EPOLLIN` | Available for read |
275 +-------------------------+-----------------------------------------------+
276 | :const:`EPOLLOUT` | Available for write |
277 +-------------------------+-----------------------------------------------+
278 | :const:`EPOLLPRI` | Urgent data for read |
279 +-------------------------+-----------------------------------------------+
280 | :const:`EPOLLERR` | Error condition happened on the assoc. fd |
281 +-------------------------+-----------------------------------------------+
282 | :const:`EPOLLHUP` | Hang up happened on the assoc. fd |
283 +-------------------------+-----------------------------------------------+
284 | :const:`EPOLLET` | Set Edge Trigger behavior, the default is |
285 | | Level Trigger behavior |
286 +-------------------------+-----------------------------------------------+
287 | :const:`EPOLLONESHOT` | Set one-shot behavior. After one event is |
288 | | pulled out, the fd is internally disabled |
289 +-------------------------+-----------------------------------------------+
290 | :const:`EPOLLEXCLUSIVE` | Wake only one epoll object when the |
291 | | associated fd has an event. The default (if |
292 | | this flag is not set) is to wake all epoll |
NAKAMURA Osamu3e0f1fc2017-04-12 19:30:40 +0900293 | | objects polling on a fd. |
Benjamin Peterson0715ce32016-07-18 22:02:44 -0700294 +-------------------------+-----------------------------------------------+
295 | :const:`EPOLLRDHUP` | Stream socket peer closed connection or shut |
296 | | down writing half of connection. |
297 +-------------------------+-----------------------------------------------+
298 | :const:`EPOLLRDNORM` | Equivalent to :const:`EPOLLIN` |
299 +-------------------------+-----------------------------------------------+
300 | :const:`EPOLLRDBAND` | Priority data band can be read. |
301 +-------------------------+-----------------------------------------------+
302 | :const:`EPOLLWRNORM` | Equivalent to :const:`EPOLLOUT` |
303 +-------------------------+-----------------------------------------------+
304 | :const:`EPOLLWRBAND` | Priority data may be written. |
305 +-------------------------+-----------------------------------------------+
306 | :const:`EPOLLMSG` | Ignored. |
307 +-------------------------+-----------------------------------------------+
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000308
309
310.. method:: epoll.close()
311
312 Close the control file descriptor of the epoll object.
313
314
Victor Stinner13423c32013-08-22 00:19:50 +0200315.. attribute:: epoll.closed
316
317 ``True`` if the epoll object is closed.
318
319
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000320.. method:: epoll.fileno()
321
322 Return the file descriptor number of the control fd.
323
324
325.. method:: epoll.fromfd(fd)
326
327 Create an epoll object from a given file descriptor.
328
329
330.. method:: epoll.register(fd[, eventmask])
331
332 Register a fd descriptor with the epoll object.
333
334
335.. method:: epoll.modify(fd, eventmask)
336
Georg Brandl632c8122014-01-12 18:03:12 +0100337 Modify a registered file descriptor.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000338
339
340.. method:: epoll.unregister(fd)
341
342 Remove a registered file descriptor from the epoll object.
343
344
Hynek Schlawackdfa46522012-05-21 11:01:54 +0200345.. method:: epoll.poll(timeout=-1, maxevents=-1)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000346
347 Wait for events. timeout in seconds (float)
348
Victor Stinner41eba222015-03-30 21:59:21 +0200349 .. versionchanged:: 3.5
350 The function is now retried with a recomputed timeout when interrupted by
351 a signal, except if the signal handler raises an exception (see
352 :pep:`475` for the rationale), instead of raising
353 :exc:`InterruptedError`.
354
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000355
Georg Brandl116aa622007-08-15 14:28:22 +0000356.. _poll-objects:
357
358Polling Objects
359---------------
360
Georg Brandl60203b42010-10-06 10:11:56 +0000361The :c:func:`poll` system call, supported on most Unix systems, provides better
Georg Brandl116aa622007-08-15 14:28:22 +0000362scalability for network servers that service many, many clients at the same
Georg Brandl60203b42010-10-06 10:11:56 +0000363time. :c:func:`poll` scales better because the system call only requires listing
364the file descriptors of interest, while :c:func:`select` builds a bitmap, turns
Georg Brandl116aa622007-08-15 14:28:22 +0000365on bits for the fds of interest, and then afterward the whole bitmap has to be
Georg Brandl60203b42010-10-06 10:11:56 +0000366linearly scanned again. :c:func:`select` is O(highest file descriptor), while
367:c:func:`poll` is O(number of file descriptors).
Georg Brandl116aa622007-08-15 14:28:22 +0000368
369
370.. method:: poll.register(fd[, eventmask])
371
372 Register a file descriptor with the polling object. Future calls to the
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300373 :meth:`poll` method will then check whether the file descriptor has any
374 pending I/O events. *fd* can be either an integer, or an object with a
375 :meth:`~io.IOBase.fileno` method that returns an integer. File objects
376 implement :meth:`!fileno`, so they can also be used as the argument.
Georg Brandl116aa622007-08-15 14:28:22 +0000377
378 *eventmask* is an optional bitmask describing the type of events you want to
379 check for, and can be a combination of the constants :const:`POLLIN`,
380 :const:`POLLPRI`, and :const:`POLLOUT`, described in the table below. If not
381 specified, the default value used will check for all 3 types of events.
382
383 +-------------------+------------------------------------------+
384 | Constant | Meaning |
385 +===================+==========================================+
386 | :const:`POLLIN` | There is data to read |
387 +-------------------+------------------------------------------+
388 | :const:`POLLPRI` | There is urgent data to read |
389 +-------------------+------------------------------------------+
390 | :const:`POLLOUT` | Ready for output: writing will not block |
391 +-------------------+------------------------------------------+
392 | :const:`POLLERR` | Error condition of some sort |
393 +-------------------+------------------------------------------+
394 | :const:`POLLHUP` | Hung up |
395 +-------------------+------------------------------------------+
Berker Peksagfe8d9662016-07-19 21:09:26 +0300396 | :const:`POLLRDHUP`| Stream socket peer closed connection, or |
397 | | shut down writing half of connection |
398 +-------------------+------------------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +0000399 | :const:`POLLNVAL` | Invalid request: descriptor not open |
400 +-------------------+------------------------------------------+
401
402 Registering a file descriptor that's already registered is not an error, and has
403 the same effect as registering the descriptor exactly once.
404
405
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000406.. method:: poll.modify(fd, eventmask)
407
408 Modifies an already registered fd. This has the same effect as
Jean-Paul Calderone7f94f392010-07-18 16:13:27 +0000409 ``register(fd, eventmask)``. Attempting to modify a file descriptor
Andrew Svetlov050f9ea2014-04-01 00:23:23 +0300410 that was never registered causes an :exc:`OSError` exception with errno
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000411 :const:`ENOENT` to be raised.
412
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000413
Georg Brandl116aa622007-08-15 14:28:22 +0000414.. method:: poll.unregister(fd)
415
416 Remove a file descriptor being tracked by a polling object. Just like the
417 :meth:`register` method, *fd* can be an integer or an object with a
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300418 :meth:`~io.IOBase.fileno` method that returns an integer.
Georg Brandl116aa622007-08-15 14:28:22 +0000419
420 Attempting to remove a file descriptor that was never registered causes a
421 :exc:`KeyError` exception to be raised.
422
423
424.. method:: poll.poll([timeout])
425
426 Polls the set of registered file descriptors, and returns a possibly-empty list
427 containing ``(fd, event)`` 2-tuples for the descriptors that have events or
428 errors to report. *fd* is the file descriptor, and *event* is a bitmask with
429 bits set for the reported events for that descriptor --- :const:`POLLIN` for
430 waiting input, :const:`POLLOUT` to indicate that the descriptor can be written
431 to, and so forth. An empty list indicates that the call timed out and no file
432 descriptors had any events to report. If *timeout* is given, it specifies the
433 length of time in milliseconds which the system will wait for events before
434 returning. If *timeout* is omitted, negative, or :const:`None`, the call will
435 block until there is an event for this poll object.
436
Victor Stinner3c7d6e02015-03-30 21:38:00 +0200437 .. versionchanged:: 3.5
438 The function is now retried with a recomputed timeout when interrupted by
439 a signal, except if the signal handler raises an exception (see
440 :pep:`475` for the rationale), instead of raising
441 :exc:`InterruptedError`.
442
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000443
444.. _kqueue-objects:
445
446Kqueue Objects
447--------------
448
449.. method:: kqueue.close()
450
451 Close the control file descriptor of the kqueue object.
452
453
Victor Stinner13423c32013-08-22 00:19:50 +0200454.. attribute:: kqueue.closed
455
456 ``True`` if the kqueue object is closed.
457
458
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000459.. method:: kqueue.fileno()
460
461 Return the file descriptor number of the control fd.
462
463
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000464.. method:: kqueue.fromfd(fd)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000465
466 Create a kqueue object from a given file descriptor.
467
468
Hynek Schlawack979f37a2012-05-22 16:12:18 +0200469.. method:: kqueue.control(changelist, max_events[, timeout=None]) -> eventlist
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000470
471 Low level interface to kevent
472
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300473 - changelist must be an iterable of kevent object or ``None``
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000474 - max_events must be 0 or a positive integer
475 - timeout in seconds (floats possible)
476
Victor Stinner4448c082015-03-31 11:48:34 +0200477 .. versionchanged:: 3.5
478 The function is now retried with a recomputed timeout when interrupted by
479 a signal, except if the signal handler raises an exception (see
480 :pep:`475` for the rationale), instead of raising
481 :exc:`InterruptedError`.
482
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000483
484.. _kevent-objects:
485
486Kevent Objects
487--------------
488
Serhiy Storchaka6dff0202016-05-07 10:49:07 +0300489https://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000490
Christian Heimesfe337bf2008-03-23 21:54:12 +0000491.. attribute:: kevent.ident
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000492
493 Value used to identify the event. The interpretation depends on the filter
494 but it's usually the file descriptor. In the constructor ident can either
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300495 be an int or an object with a :meth:`~io.IOBase.fileno` method. kevent
496 stores the integer internally.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000497
Christian Heimesfe337bf2008-03-23 21:54:12 +0000498.. attribute:: kevent.filter
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000499
Georg Brandl1b5ab452009-08-13 07:56:35 +0000500 Name of the kernel filter.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000501
502 +---------------------------+---------------------------------------------+
503 | Constant | Meaning |
504 +===========================+=============================================+
505 | :const:`KQ_FILTER_READ` | Takes a descriptor and returns whenever |
506 | | there is data available to read |
507 +---------------------------+---------------------------------------------+
508 | :const:`KQ_FILTER_WRITE` | Takes a descriptor and returns whenever |
Georg Brandl1b5ab452009-08-13 07:56:35 +0000509 | | there is data available to write |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000510 +---------------------------+---------------------------------------------+
511 | :const:`KQ_FILTER_AIO` | AIO requests |
512 +---------------------------+---------------------------------------------+
513 | :const:`KQ_FILTER_VNODE` | Returns when one or more of the requested |
514 | | events watched in *fflag* occurs |
515 +---------------------------+---------------------------------------------+
516 | :const:`KQ_FILTER_PROC` | Watch for events on a process id |
517 +---------------------------+---------------------------------------------+
518 | :const:`KQ_FILTER_NETDEV` | Watch for events on a network device |
519 | | [not available on Mac OS X] |
520 +---------------------------+---------------------------------------------+
521 | :const:`KQ_FILTER_SIGNAL` | Returns whenever the watched signal is |
522 | | delivered to the process |
523 +---------------------------+---------------------------------------------+
524 | :const:`KQ_FILTER_TIMER` | Establishes an arbitrary timer |
525 +---------------------------+---------------------------------------------+
526
Christian Heimesfe337bf2008-03-23 21:54:12 +0000527.. attribute:: kevent.flags
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000528
Georg Brandl1b5ab452009-08-13 07:56:35 +0000529 Filter action.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000530
531 +---------------------------+---------------------------------------------+
532 | Constant | Meaning |
533 +===========================+=============================================+
534 | :const:`KQ_EV_ADD` | Adds or modifies an event |
535 +---------------------------+---------------------------------------------+
536 | :const:`KQ_EV_DELETE` | Removes an event from the queue |
537 +---------------------------+---------------------------------------------+
538 | :const:`KQ_EV_ENABLE` | Permitscontrol() to returns the event |
539 +---------------------------+---------------------------------------------+
540 | :const:`KQ_EV_DISABLE` | Disablesevent |
541 +---------------------------+---------------------------------------------+
Georg Brandl2ee470f2008-07-16 12:55:28 +0000542 | :const:`KQ_EV_ONESHOT` | Removes event after first occurrence |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000543 +---------------------------+---------------------------------------------+
544 | :const:`KQ_EV_CLEAR` | Reset the state after an event is retrieved |
545 +---------------------------+---------------------------------------------+
546 | :const:`KQ_EV_SYSFLAGS` | internal event |
547 +---------------------------+---------------------------------------------+
548 | :const:`KQ_EV_FLAG1` | internal event |
549 +---------------------------+---------------------------------------------+
550 | :const:`KQ_EV_EOF` | Filter specific EOF condition |
551 +---------------------------+---------------------------------------------+
552 | :const:`KQ_EV_ERROR` | See return values |
553 +---------------------------+---------------------------------------------+
554
555
Christian Heimesfe337bf2008-03-23 21:54:12 +0000556.. attribute:: kevent.fflags
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000557
Georg Brandl1b5ab452009-08-13 07:56:35 +0000558 Filter specific flags.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000559
Georg Brandl1b5ab452009-08-13 07:56:35 +0000560 :const:`KQ_FILTER_READ` and :const:`KQ_FILTER_WRITE` filter flags:
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000561
562 +----------------------------+--------------------------------------------+
563 | Constant | Meaning |
564 +============================+============================================+
565 | :const:`KQ_NOTE_LOWAT` | low water mark of a socket buffer |
566 +----------------------------+--------------------------------------------+
567
Georg Brandl1b5ab452009-08-13 07:56:35 +0000568 :const:`KQ_FILTER_VNODE` filter flags:
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000569
570 +----------------------------+--------------------------------------------+
571 | Constant | Meaning |
572 +============================+============================================+
573 | :const:`KQ_NOTE_DELETE` | *unlink()* was called |
574 +----------------------------+--------------------------------------------+
Georg Brandl2ee470f2008-07-16 12:55:28 +0000575 | :const:`KQ_NOTE_WRITE` | a write occurred |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000576 +----------------------------+--------------------------------------------+
577 | :const:`KQ_NOTE_EXTEND` | the file was extended |
578 +----------------------------+--------------------------------------------+
579 | :const:`KQ_NOTE_ATTRIB` | an attribute was changed |
580 +----------------------------+--------------------------------------------+
581 | :const:`KQ_NOTE_LINK` | the link count has changed |
582 +----------------------------+--------------------------------------------+
583 | :const:`KQ_NOTE_RENAME` | the file was renamed |
584 +----------------------------+--------------------------------------------+
585 | :const:`KQ_NOTE_REVOKE` | access to the file was revoked |
586 +----------------------------+--------------------------------------------+
587
Georg Brandl1b5ab452009-08-13 07:56:35 +0000588 :const:`KQ_FILTER_PROC` filter flags:
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000589
590 +----------------------------+--------------------------------------------+
591 | Constant | Meaning |
592 +============================+============================================+
593 | :const:`KQ_NOTE_EXIT` | the process has exited |
594 +----------------------------+--------------------------------------------+
595 | :const:`KQ_NOTE_FORK` | the process has called *fork()* |
596 +----------------------------+--------------------------------------------+
597 | :const:`KQ_NOTE_EXEC` | the process has executed a new process |
598 +----------------------------+--------------------------------------------+
599 | :const:`KQ_NOTE_PCTRLMASK` | internal filter flag |
600 +----------------------------+--------------------------------------------+
601 | :const:`KQ_NOTE_PDATAMASK` | internal filter flag |
602 +----------------------------+--------------------------------------------+
603 | :const:`KQ_NOTE_TRACK` | follow a process across *fork()* |
604 +----------------------------+--------------------------------------------+
605 | :const:`KQ_NOTE_CHILD` | returned on the child process for |
606 | | *NOTE_TRACK* |
607 +----------------------------+--------------------------------------------+
608 | :const:`KQ_NOTE_TRACKERR` | unable to attach to a child |
609 +----------------------------+--------------------------------------------+
610
Georg Brandl1b5ab452009-08-13 07:56:35 +0000611 :const:`KQ_FILTER_NETDEV` filter flags (not available on Mac OS X):
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000612
613 +----------------------------+--------------------------------------------+
614 | Constant | Meaning |
615 +============================+============================================+
616 | :const:`KQ_NOTE_LINKUP` | link is up |
617 +----------------------------+--------------------------------------------+
618 | :const:`KQ_NOTE_LINKDOWN` | link is down |
619 +----------------------------+--------------------------------------------+
620 | :const:`KQ_NOTE_LINKINV` | link state is invalid |
621 +----------------------------+--------------------------------------------+
622
623
Christian Heimesfe337bf2008-03-23 21:54:12 +0000624.. attribute:: kevent.data
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000625
Georg Brandl1b5ab452009-08-13 07:56:35 +0000626 Filter specific data.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000627
628
Christian Heimesfe337bf2008-03-23 21:54:12 +0000629.. attribute:: kevent.udata
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000630
Georg Brandl1b5ab452009-08-13 07:56:35 +0000631 User defined value.