blob: a62dc844e4f39aafbf34d46abccdc596072b9224 [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
7
Georg Brandl60203b42010-10-06 10:11:56 +00008This module provides access to the :c:func:`select` and :c:func:`poll` functions
Jesus Cead8b9ae62011-11-14 19:07:41 +01009available in most operating systems, :c:func:`devpoll` available on
10Solaris and derivatives, :c:func:`epoll` available on Linux 2.5+ and
Georg Brandl60203b42010-10-06 10:11:56 +000011:c:func:`kqueue` available on most BSD.
Christian Heimes4fbc72b2008-03-22 00:47:35 +000012Note that on Windows, it only works for sockets; on other operating systems,
13it also works for other file types (in particular, on Unix, it works on pipes).
14It cannot be used on regular files to determine whether a file has grown since
15it was last read.
Georg Brandl116aa622007-08-15 14:28:22 +000016
Victor Stinner73821c42013-09-04 20:40:13 +020017.. note::
18
19 The :mod:`selectors` module allows high-level and efficient I/O
20 multiplexing, built upon the :mod:`select` module primitives. Users are
21 encouraged to use the :mod:`selectors` module instead, unless they want
22 precise control over the OS-level primitives used.
23
24
Georg Brandl116aa622007-08-15 14:28:22 +000025The module defines the following:
26
27
28.. exception:: error
29
Antoine Pitrou9b7fcf82011-10-12 16:23:02 +020030 A deprecated alias of :exc:`OSError`.
31
32 .. versionchanged:: 3.3
33 Following :pep:`3151`, this class was made an alias of :exc:`OSError`.
Georg Brandl116aa622007-08-15 14:28:22 +000034
35
Jesus Cead8b9ae62011-11-14 19:07:41 +010036.. function:: devpoll()
Jesus Ceaf450c1b2011-11-15 05:42:59 +010037
Jesus Cead8b9ae62011-11-14 19:07:41 +010038 (Only supported on Solaris and derivatives.) Returns a ``/dev/poll``
39 polling object; see section :ref:`devpoll-objects` below for the
40 methods supported by devpoll objects.
41
42 :c:func:`devpoll` objects are linked to the number of file
43 descriptors allowed at the time of instantiation. If your program
44 reduces this value, :c:func:`devpoll` will fail. If your program
Jesus Ceaf450c1b2011-11-15 05:42:59 +010045 increases this value, :c:func:`devpoll` may return an
Jesus Cead8b9ae62011-11-14 19:07:41 +010046 incomplete list of active file descriptors.
47
Victor Stinnerdaf45552013-08-28 00:53:59 +020048 The new file descriptor is :ref:`non-inheritable <fd_inheritance>`.
49
Jesus Cead8b9ae62011-11-14 19:07:41 +010050 .. versionadded:: 3.3
51
Victor Stinnerdaf45552013-08-28 00:53:59 +020052 .. versionchanged:: 3.4
53 The new file descriptor is now non-inheritable.
54
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -060055.. function:: epoll(sizehint=-1, flags=0)
Christian Heimes4fbc72b2008-03-22 00:47:35 +000056
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -060057 (Only supported on Linux 2.5.44 and newer.) Return an edge polling object,
58 which can be used as Edge or Level Triggered interface for I/O
59 events. *sizehint* is deprecated and completely ignored. *flags* can be set
60 to :const:`EPOLL_CLOEXEC`, which causes the epoll descriptor to be closed
R David Murray2bc930f2013-12-31 11:17:21 -050061 automatically when :func:`os.execve` is called.
62
63 See the :ref:`epoll-objects` section below for the methods supported by
64 epolling objects.
65
66 ``epoll`` objects support the context management protocol: when used in a
67 :keyword:`with` statement, the new file descriptor is automatically closed
68 at the end of the block.
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -060069
Victor Stinnerdaf45552013-08-28 00:53:59 +020070 The new file descriptor is :ref:`non-inheritable <fd_inheritance>`.
71
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -060072 .. versionchanged:: 3.3
Benjamin Peterson2fb9ae92011-12-27 15:15:41 -060073 Added the *flags* parameter.
Christian Heimes4fbc72b2008-03-22 00:47:35 +000074
Antoine Pitrou09bb89b2012-12-15 21:14:21 +010075 .. versionchanged:: 3.4
76 Support for the :keyword:`with` statement was added.
Victor Stinnerdaf45552013-08-28 00:53:59 +020077 The new file descriptor is now non-inheritable.
Antoine Pitrou09bb89b2012-12-15 21:14:21 +010078
Christian Heimes4fbc72b2008-03-22 00:47:35 +000079
Georg Brandl116aa622007-08-15 14:28:22 +000080.. function:: poll()
81
82 (Not supported by all operating systems.) Returns a polling object, which
83 supports registering and unregistering file descriptors, and then polling them
84 for I/O events; see section :ref:`poll-objects` below for the methods supported
85 by polling objects.
86
87
Christian Heimesfe337bf2008-03-23 21:54:12 +000088.. function:: kqueue()
Christian Heimes4fbc72b2008-03-22 00:47:35 +000089
Georg Brandle767e042010-07-14 08:00:22 +000090 (Only supported on BSD.) Returns a kernel queue object; see section
Christian Heimesfe337bf2008-03-23 21:54:12 +000091 :ref:`kqueue-objects` below for the methods supported by kqueue objects.
Christian Heimes4fbc72b2008-03-22 00:47:35 +000092
Victor Stinnerdaf45552013-08-28 00:53:59 +020093 The new file descriptor is :ref:`non-inheritable <fd_inheritance>`.
94
95 .. versionchanged:: 3.4
96 The new file descriptor is now non-inheritable.
97
Christian Heimes4fbc72b2008-03-22 00:47:35 +000098
Benjamin Peterson1baf4652009-12-31 03:11:23 +000099.. function:: kevent(ident, filter=KQ_FILTER_READ, flags=KQ_EV_ADD, fflags=0, data=0, udata=0)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000100
Georg Brandle767e042010-07-14 08:00:22 +0000101 (Only supported on BSD.) Returns a kernel event object; see section
102 :ref:`kevent-objects` below for the methods supported by kevent objects.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000103
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000104
Georg Brandl734e2682008-08-12 08:18:18 +0000105.. function:: select(rlist, wlist, xlist[, timeout])
Georg Brandl116aa622007-08-15 14:28:22 +0000106
Georg Brandl60203b42010-10-06 10:11:56 +0000107 This is a straightforward interface to the Unix :c:func:`select` system call.
Georg Brandl116aa622007-08-15 14:28:22 +0000108 The first three arguments are sequences of 'waitable objects': either
109 integers representing file descriptors or objects with a parameterless method
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300110 named :meth:`~io.IOBase.fileno` returning such an integer:
Georg Brandl734e2682008-08-12 08:18:18 +0000111
112 * *rlist*: wait until ready for reading
113 * *wlist*: wait until ready for writing
114 * *xlist*: wait for an "exceptional condition" (see the manual page for what
115 your system considers such a condition)
116
117 Empty sequences are allowed, but acceptance of three empty sequences is
118 platform-dependent. (It is known to work on Unix but not on Windows.) The
119 optional *timeout* argument specifies a time-out as a floating point number
120 in seconds. When the *timeout* argument is omitted the function blocks until
121 at least one file descriptor is ready. A time-out value of zero specifies a
122 poll and never blocks.
Georg Brandl116aa622007-08-15 14:28:22 +0000123
124 The return value is a triple of lists of objects that are ready: subsets of the
125 first three arguments. When the time-out is reached without a file descriptor
126 becoming ready, three empty lists are returned.
127
128 .. index::
129 single: socket() (in module socket)
130 single: popen() (in module os)
131
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000132 Among the acceptable object types in the sequences are Python :term:`file
133 objects <file object>` (e.g. ``sys.stdin``, or objects returned by
134 :func:`open` or :func:`os.popen`), socket objects returned by
135 :func:`socket.socket`. You may also define a :dfn:`wrapper` class yourself,
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300136 as long as it has an appropriate :meth:`~io.IOBase.fileno` method (that
137 really returns a file descriptor, not just a random integer).
Georg Brandl116aa622007-08-15 14:28:22 +0000138
Georg Brandl116aa622007-08-15 14:28:22 +0000139 .. note::
140
141 .. index:: single: WinSock
142
Georg Brandl734e2682008-08-12 08:18:18 +0000143 File objects on Windows are not acceptable, but sockets are. On Windows,
Georg Brandl60203b42010-10-06 10:11:56 +0000144 the underlying :c:func:`select` function is provided by the WinSock
Georg Brandl734e2682008-08-12 08:18:18 +0000145 library, and does not handle file descriptors that don't originate from
146 WinSock.
Georg Brandl116aa622007-08-15 14:28:22 +0000147
Victor Stinnerf70e1ca2015-03-30 21:16:11 +0200148 .. versionchanged:: 3.5
149 The function is now retried with a recomputed timeout when interrupted by
150 a signal, except if the signal handler raises an exception (see
151 :pep:`475` for the rationale), instead of raising
152 :exc:`InterruptedError`.
153
154
Antoine Pitroucfad97b2011-01-25 17:24:57 +0000155.. attribute:: PIPE_BUF
Gregory P. Smithb970b862009-07-04 02:28:47 +0000156
Antoine Pitrouda7be3e2011-01-25 16:28:44 +0000157 The minimum number of bytes which can be written without blocking to a pipe
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300158 when the pipe has been reported as ready for writing by :func:`~select.select`,
Antoine Pitrouda7be3e2011-01-25 16:28:44 +0000159 :func:`poll` or another interface in this module. This doesn't apply
160 to other kind of file-like objects such as sockets.
161
Amaury Forgeot d'Arcace31022009-07-09 22:44:11 +0000162 This value is guaranteed by POSIX to be at least 512. Availability: Unix.
Gregory P. Smithb970b862009-07-04 02:28:47 +0000163
Mark Dickinson574b1d62009-10-01 20:20:09 +0000164 .. versionadded:: 3.2
Gregory P. Smithb970b862009-07-04 02:28:47 +0000165
Georg Brandl116aa622007-08-15 14:28:22 +0000166
Jesus Cead8b9ae62011-11-14 19:07:41 +0100167.. _devpoll-objects:
168
169``/dev/poll`` Polling Objects
Georg Brandl525d3552014-10-29 10:26:56 +0100170-----------------------------
Jesus Cead8b9ae62011-11-14 19:07:41 +0100171
172Solaris and derivatives have ``/dev/poll``. While :c:func:`select` is
173O(highest file descriptor) and :c:func:`poll` is O(number of file
174descriptors), ``/dev/poll`` is O(active file descriptors).
175
176``/dev/poll`` behaviour is very close to the standard :c:func:`poll`
177object.
178
179
Victor Stinner13423c32013-08-22 00:19:50 +0200180.. method:: devpoll.close()
181
182 Close the file descriptor of the polling object.
183
184 .. versionadded:: 3.4
185
186
187.. attribute:: devpoll.closed
188
189 ``True`` if the polling object is closed.
190
191 .. versionadded:: 3.4
192
193
194.. method:: devpoll.fileno()
195
196 Return the file descriptor number of the polling object.
197
198 .. versionadded:: 3.4
199
200
Jesus Cead8b9ae62011-11-14 19:07:41 +0100201.. method:: devpoll.register(fd[, eventmask])
202
203 Register a file descriptor with the polling object. Future calls to the
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300204 :meth:`poll` method will then check whether the file descriptor has any
205 pending I/O events. *fd* can be either an integer, or an object with a
206 :meth:`~io.IOBase.fileno` method that returns an integer. File objects
207 implement :meth:`!fileno`, so they can also be used as the argument.
Jesus Cead8b9ae62011-11-14 19:07:41 +0100208
209 *eventmask* is an optional bitmask describing the type of events you want to
210 check for. The constants are the same that with :c:func:`poll`
211 object. The default value is a combination of the constants :const:`POLLIN`,
212 :const:`POLLPRI`, and :const:`POLLOUT`.
213
214 .. warning::
215
216 Registering a file descriptor that's already registered is not an
Donald Stufft8b852f12014-05-20 12:58:38 -0400217 error, but the result is undefined. The appropriate action is to
Jesus Cead8b9ae62011-11-14 19:07:41 +0100218 unregister or modify it first. This is an important difference
219 compared with :c:func:`poll`.
220
221
222.. method:: devpoll.modify(fd[, eventmask])
223
224 This method does an :meth:`unregister` followed by a
225 :meth:`register`. It is (a bit) more efficient that doing the same
226 explicitly.
227
228
229.. method:: devpoll.unregister(fd)
230
231 Remove a file descriptor being tracked by a polling object. Just like the
232 :meth:`register` method, *fd* can be an integer or an object with a
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300233 :meth:`~io.IOBase.fileno` method that returns an integer.
Jesus Cead8b9ae62011-11-14 19:07:41 +0100234
235 Attempting to remove a file descriptor that was never registered is
236 safely ignored.
237
238
239.. method:: devpoll.poll([timeout])
240
241 Polls the set of registered file descriptors, and returns a possibly-empty list
242 containing ``(fd, event)`` 2-tuples for the descriptors that have events or
243 errors to report. *fd* is the file descriptor, and *event* is a bitmask with
244 bits set for the reported events for that descriptor --- :const:`POLLIN` for
245 waiting input, :const:`POLLOUT` to indicate that the descriptor can be written
246 to, and so forth. An empty list indicates that the call timed out and no file
247 descriptors had any events to report. If *timeout* is given, it specifies the
248 length of time in milliseconds which the system will wait for events before
249 returning. If *timeout* is omitted, -1, or :const:`None`, the call will
250 block until there is an event for this poll object.
251
Victor Stinner45ca48b2015-03-31 12:10:33 +0200252 .. versionchanged:: 3.5
253 The function is now retried with a recomputed timeout when interrupted by
254 a signal, except if the signal handler raises an exception (see
255 :pep:`475` for the rationale), instead of raising
256 :exc:`InterruptedError`.
257
Jesus Cead8b9ae62011-11-14 19:07:41 +0100258
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000259.. _epoll-objects:
260
261Edge and Level Trigger Polling (epoll) Objects
262----------------------------------------------
263
264 http://linux.die.net/man/4/epoll
265
266 *eventmask*
267
268 +-----------------------+-----------------------------------------------+
269 | Constant | Meaning |
270 +=======================+===============================================+
271 | :const:`EPOLLIN` | Available for read |
272 +-----------------------+-----------------------------------------------+
273 | :const:`EPOLLOUT` | Available for write |
274 +-----------------------+-----------------------------------------------+
275 | :const:`EPOLLPRI` | Urgent data for read |
276 +-----------------------+-----------------------------------------------+
Christian Heimes5e696852008-04-09 08:37:03 +0000277 | :const:`EPOLLERR` | Error condition happened on the assoc. fd |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000278 +-----------------------+-----------------------------------------------+
Christian Heimes5e696852008-04-09 08:37:03 +0000279 | :const:`EPOLLHUP` | Hang up happened on the assoc. fd |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000280 +-----------------------+-----------------------------------------------+
281 | :const:`EPOLLET` | Set Edge Trigger behavior, the default is |
282 | | Level Trigger behavior |
283 +-----------------------+-----------------------------------------------+
284 | :const:`EPOLLONESHOT` | Set one-shot behavior. After one event is |
285 | | pulled out, the fd is internally disabled |
286 +-----------------------+-----------------------------------------------+
Jean-Paul Calderone7f54dce2010-07-18 16:30:31 +0000287 | :const:`EPOLLRDNORM` | Equivalent to :const:`EPOLLIN` |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000288 +-----------------------+-----------------------------------------------+
Jean-Paul Calderone7f54dce2010-07-18 16:30:31 +0000289 | :const:`EPOLLRDBAND` | Priority data band can be read. |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000290 +-----------------------+-----------------------------------------------+
Jean-Paul Calderone7f54dce2010-07-18 16:30:31 +0000291 | :const:`EPOLLWRNORM` | Equivalent to :const:`EPOLLOUT` |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000292 +-----------------------+-----------------------------------------------+
Jean-Paul Calderone7f54dce2010-07-18 16:30:31 +0000293 | :const:`EPOLLWRBAND` | Priority data may be written. |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000294 +-----------------------+-----------------------------------------------+
Jean-Paul Calderone7f54dce2010-07-18 16:30:31 +0000295 | :const:`EPOLLMSG` | Ignored. |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000296 +-----------------------+-----------------------------------------------+
297
298
299.. method:: epoll.close()
300
301 Close the control file descriptor of the epoll object.
302
303
Victor Stinner13423c32013-08-22 00:19:50 +0200304.. attribute:: epoll.closed
305
306 ``True`` if the epoll object is closed.
307
308
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000309.. method:: epoll.fileno()
310
311 Return the file descriptor number of the control fd.
312
313
314.. method:: epoll.fromfd(fd)
315
316 Create an epoll object from a given file descriptor.
317
318
319.. method:: epoll.register(fd[, eventmask])
320
321 Register a fd descriptor with the epoll object.
322
323
324.. method:: epoll.modify(fd, eventmask)
325
Georg Brandl632c8122014-01-12 18:03:12 +0100326 Modify a registered file descriptor.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000327
328
329.. method:: epoll.unregister(fd)
330
331 Remove a registered file descriptor from the epoll object.
332
333
Hynek Schlawackdfa46522012-05-21 11:01:54 +0200334.. method:: epoll.poll(timeout=-1, maxevents=-1)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000335
336 Wait for events. timeout in seconds (float)
337
Victor Stinner41eba222015-03-30 21:59:21 +0200338 .. versionchanged:: 3.5
339 The function is now retried with a recomputed timeout when interrupted by
340 a signal, except if the signal handler raises an exception (see
341 :pep:`475` for the rationale), instead of raising
342 :exc:`InterruptedError`.
343
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000344
Georg Brandl116aa622007-08-15 14:28:22 +0000345.. _poll-objects:
346
347Polling Objects
348---------------
349
Georg Brandl60203b42010-10-06 10:11:56 +0000350The :c:func:`poll` system call, supported on most Unix systems, provides better
Georg Brandl116aa622007-08-15 14:28:22 +0000351scalability for network servers that service many, many clients at the same
Georg Brandl60203b42010-10-06 10:11:56 +0000352time. :c:func:`poll` scales better because the system call only requires listing
353the file descriptors of interest, while :c:func:`select` builds a bitmap, turns
Georg Brandl116aa622007-08-15 14:28:22 +0000354on bits for the fds of interest, and then afterward the whole bitmap has to be
Georg Brandl60203b42010-10-06 10:11:56 +0000355linearly scanned again. :c:func:`select` is O(highest file descriptor), while
356:c:func:`poll` is O(number of file descriptors).
Georg Brandl116aa622007-08-15 14:28:22 +0000357
358
359.. method:: poll.register(fd[, eventmask])
360
361 Register a file descriptor with the polling object. Future calls to the
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300362 :meth:`poll` method will then check whether the file descriptor has any
363 pending I/O events. *fd* can be either an integer, or an object with a
364 :meth:`~io.IOBase.fileno` method that returns an integer. File objects
365 implement :meth:`!fileno`, so they can also be used as the argument.
Georg Brandl116aa622007-08-15 14:28:22 +0000366
367 *eventmask* is an optional bitmask describing the type of events you want to
368 check for, and can be a combination of the constants :const:`POLLIN`,
369 :const:`POLLPRI`, and :const:`POLLOUT`, described in the table below. If not
370 specified, the default value used will check for all 3 types of events.
371
372 +-------------------+------------------------------------------+
373 | Constant | Meaning |
374 +===================+==========================================+
375 | :const:`POLLIN` | There is data to read |
376 +-------------------+------------------------------------------+
377 | :const:`POLLPRI` | There is urgent data to read |
378 +-------------------+------------------------------------------+
379 | :const:`POLLOUT` | Ready for output: writing will not block |
380 +-------------------+------------------------------------------+
381 | :const:`POLLERR` | Error condition of some sort |
382 +-------------------+------------------------------------------+
383 | :const:`POLLHUP` | Hung up |
384 +-------------------+------------------------------------------+
385 | :const:`POLLNVAL` | Invalid request: descriptor not open |
386 +-------------------+------------------------------------------+
387
388 Registering a file descriptor that's already registered is not an error, and has
389 the same effect as registering the descriptor exactly once.
390
391
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000392.. method:: poll.modify(fd, eventmask)
393
394 Modifies an already registered fd. This has the same effect as
Jean-Paul Calderone7f94f392010-07-18 16:13:27 +0000395 ``register(fd, eventmask)``. Attempting to modify a file descriptor
Andrew Svetlov050f9ea2014-04-01 00:23:23 +0300396 that was never registered causes an :exc:`OSError` exception with errno
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000397 :const:`ENOENT` to be raised.
398
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000399
Georg Brandl116aa622007-08-15 14:28:22 +0000400.. method:: poll.unregister(fd)
401
402 Remove a file descriptor being tracked by a polling object. Just like the
403 :meth:`register` method, *fd* can be an integer or an object with a
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300404 :meth:`~io.IOBase.fileno` method that returns an integer.
Georg Brandl116aa622007-08-15 14:28:22 +0000405
406 Attempting to remove a file descriptor that was never registered causes a
407 :exc:`KeyError` exception to be raised.
408
409
410.. method:: poll.poll([timeout])
411
412 Polls the set of registered file descriptors, and returns a possibly-empty list
413 containing ``(fd, event)`` 2-tuples for the descriptors that have events or
414 errors to report. *fd* is the file descriptor, and *event* is a bitmask with
415 bits set for the reported events for that descriptor --- :const:`POLLIN` for
416 waiting input, :const:`POLLOUT` to indicate that the descriptor can be written
417 to, and so forth. An empty list indicates that the call timed out and no file
418 descriptors had any events to report. If *timeout* is given, it specifies the
419 length of time in milliseconds which the system will wait for events before
420 returning. If *timeout* is omitted, negative, or :const:`None`, the call will
421 block until there is an event for this poll object.
422
Victor Stinner3c7d6e02015-03-30 21:38:00 +0200423 .. versionchanged:: 3.5
424 The function is now retried with a recomputed timeout when interrupted by
425 a signal, except if the signal handler raises an exception (see
426 :pep:`475` for the rationale), instead of raising
427 :exc:`InterruptedError`.
428
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000429
430.. _kqueue-objects:
431
432Kqueue Objects
433--------------
434
435.. method:: kqueue.close()
436
437 Close the control file descriptor of the kqueue object.
438
439
Victor Stinner13423c32013-08-22 00:19:50 +0200440.. attribute:: kqueue.closed
441
442 ``True`` if the kqueue object is closed.
443
444
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000445.. method:: kqueue.fileno()
446
447 Return the file descriptor number of the control fd.
448
449
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000450.. method:: kqueue.fromfd(fd)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000451
452 Create a kqueue object from a given file descriptor.
453
454
Hynek Schlawack979f37a2012-05-22 16:12:18 +0200455.. method:: kqueue.control(changelist, max_events[, timeout=None]) -> eventlist
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000456
457 Low level interface to kevent
458
459 - changelist must be an iterable of kevent object or None
460 - max_events must be 0 or a positive integer
461 - timeout in seconds (floats possible)
462
Victor Stinner4448c082015-03-31 11:48:34 +0200463 .. versionchanged:: 3.5
464 The function is now retried with a recomputed timeout when interrupted by
465 a signal, except if the signal handler raises an exception (see
466 :pep:`475` for the rationale), instead of raising
467 :exc:`InterruptedError`.
468
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000469
470.. _kevent-objects:
471
472Kevent Objects
473--------------
474
Christian Heimesfe337bf2008-03-23 21:54:12 +0000475http://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000476
Christian Heimesfe337bf2008-03-23 21:54:12 +0000477.. attribute:: kevent.ident
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000478
479 Value used to identify the event. The interpretation depends on the filter
480 but it's usually the file descriptor. In the constructor ident can either
Serhiy Storchaka9e0ae532013-08-24 00:23:38 +0300481 be an int or an object with a :meth:`~io.IOBase.fileno` method. kevent
482 stores the integer internally.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000483
Christian Heimesfe337bf2008-03-23 21:54:12 +0000484.. attribute:: kevent.filter
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000485
Georg Brandl1b5ab452009-08-13 07:56:35 +0000486 Name of the kernel filter.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000487
488 +---------------------------+---------------------------------------------+
489 | Constant | Meaning |
490 +===========================+=============================================+
491 | :const:`KQ_FILTER_READ` | Takes a descriptor and returns whenever |
492 | | there is data available to read |
493 +---------------------------+---------------------------------------------+
494 | :const:`KQ_FILTER_WRITE` | Takes a descriptor and returns whenever |
Georg Brandl1b5ab452009-08-13 07:56:35 +0000495 | | there is data available to write |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000496 +---------------------------+---------------------------------------------+
497 | :const:`KQ_FILTER_AIO` | AIO requests |
498 +---------------------------+---------------------------------------------+
499 | :const:`KQ_FILTER_VNODE` | Returns when one or more of the requested |
500 | | events watched in *fflag* occurs |
501 +---------------------------+---------------------------------------------+
502 | :const:`KQ_FILTER_PROC` | Watch for events on a process id |
503 +---------------------------+---------------------------------------------+
504 | :const:`KQ_FILTER_NETDEV` | Watch for events on a network device |
505 | | [not available on Mac OS X] |
506 +---------------------------+---------------------------------------------+
507 | :const:`KQ_FILTER_SIGNAL` | Returns whenever the watched signal is |
508 | | delivered to the process |
509 +---------------------------+---------------------------------------------+
510 | :const:`KQ_FILTER_TIMER` | Establishes an arbitrary timer |
511 +---------------------------+---------------------------------------------+
512
Christian Heimesfe337bf2008-03-23 21:54:12 +0000513.. attribute:: kevent.flags
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000514
Georg Brandl1b5ab452009-08-13 07:56:35 +0000515 Filter action.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000516
517 +---------------------------+---------------------------------------------+
518 | Constant | Meaning |
519 +===========================+=============================================+
520 | :const:`KQ_EV_ADD` | Adds or modifies an event |
521 +---------------------------+---------------------------------------------+
522 | :const:`KQ_EV_DELETE` | Removes an event from the queue |
523 +---------------------------+---------------------------------------------+
524 | :const:`KQ_EV_ENABLE` | Permitscontrol() to returns the event |
525 +---------------------------+---------------------------------------------+
526 | :const:`KQ_EV_DISABLE` | Disablesevent |
527 +---------------------------+---------------------------------------------+
Georg Brandl2ee470f2008-07-16 12:55:28 +0000528 | :const:`KQ_EV_ONESHOT` | Removes event after first occurrence |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000529 +---------------------------+---------------------------------------------+
530 | :const:`KQ_EV_CLEAR` | Reset the state after an event is retrieved |
531 +---------------------------+---------------------------------------------+
532 | :const:`KQ_EV_SYSFLAGS` | internal event |
533 +---------------------------+---------------------------------------------+
534 | :const:`KQ_EV_FLAG1` | internal event |
535 +---------------------------+---------------------------------------------+
536 | :const:`KQ_EV_EOF` | Filter specific EOF condition |
537 +---------------------------+---------------------------------------------+
538 | :const:`KQ_EV_ERROR` | See return values |
539 +---------------------------+---------------------------------------------+
540
541
Christian Heimesfe337bf2008-03-23 21:54:12 +0000542.. attribute:: kevent.fflags
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000543
Georg Brandl1b5ab452009-08-13 07:56:35 +0000544 Filter specific flags.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000545
Georg Brandl1b5ab452009-08-13 07:56:35 +0000546 :const:`KQ_FILTER_READ` and :const:`KQ_FILTER_WRITE` filter flags:
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000547
548 +----------------------------+--------------------------------------------+
549 | Constant | Meaning |
550 +============================+============================================+
551 | :const:`KQ_NOTE_LOWAT` | low water mark of a socket buffer |
552 +----------------------------+--------------------------------------------+
553
Georg Brandl1b5ab452009-08-13 07:56:35 +0000554 :const:`KQ_FILTER_VNODE` filter flags:
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000555
556 +----------------------------+--------------------------------------------+
557 | Constant | Meaning |
558 +============================+============================================+
559 | :const:`KQ_NOTE_DELETE` | *unlink()* was called |
560 +----------------------------+--------------------------------------------+
Georg Brandl2ee470f2008-07-16 12:55:28 +0000561 | :const:`KQ_NOTE_WRITE` | a write occurred |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000562 +----------------------------+--------------------------------------------+
563 | :const:`KQ_NOTE_EXTEND` | the file was extended |
564 +----------------------------+--------------------------------------------+
565 | :const:`KQ_NOTE_ATTRIB` | an attribute was changed |
566 +----------------------------+--------------------------------------------+
567 | :const:`KQ_NOTE_LINK` | the link count has changed |
568 +----------------------------+--------------------------------------------+
569 | :const:`KQ_NOTE_RENAME` | the file was renamed |
570 +----------------------------+--------------------------------------------+
571 | :const:`KQ_NOTE_REVOKE` | access to the file was revoked |
572 +----------------------------+--------------------------------------------+
573
Georg Brandl1b5ab452009-08-13 07:56:35 +0000574 :const:`KQ_FILTER_PROC` filter flags:
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000575
576 +----------------------------+--------------------------------------------+
577 | Constant | Meaning |
578 +============================+============================================+
579 | :const:`KQ_NOTE_EXIT` | the process has exited |
580 +----------------------------+--------------------------------------------+
581 | :const:`KQ_NOTE_FORK` | the process has called *fork()* |
582 +----------------------------+--------------------------------------------+
583 | :const:`KQ_NOTE_EXEC` | the process has executed a new process |
584 +----------------------------+--------------------------------------------+
585 | :const:`KQ_NOTE_PCTRLMASK` | internal filter flag |
586 +----------------------------+--------------------------------------------+
587 | :const:`KQ_NOTE_PDATAMASK` | internal filter flag |
588 +----------------------------+--------------------------------------------+
589 | :const:`KQ_NOTE_TRACK` | follow a process across *fork()* |
590 +----------------------------+--------------------------------------------+
591 | :const:`KQ_NOTE_CHILD` | returned on the child process for |
592 | | *NOTE_TRACK* |
593 +----------------------------+--------------------------------------------+
594 | :const:`KQ_NOTE_TRACKERR` | unable to attach to a child |
595 +----------------------------+--------------------------------------------+
596
Georg Brandl1b5ab452009-08-13 07:56:35 +0000597 :const:`KQ_FILTER_NETDEV` filter flags (not available on Mac OS X):
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000598
599 +----------------------------+--------------------------------------------+
600 | Constant | Meaning |
601 +============================+============================================+
602 | :const:`KQ_NOTE_LINKUP` | link is up |
603 +----------------------------+--------------------------------------------+
604 | :const:`KQ_NOTE_LINKDOWN` | link is down |
605 +----------------------------+--------------------------------------------+
606 | :const:`KQ_NOTE_LINKINV` | link state is invalid |
607 +----------------------------+--------------------------------------------+
608
609
Christian Heimesfe337bf2008-03-23 21:54:12 +0000610.. attribute:: kevent.data
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000611
Georg Brandl1b5ab452009-08-13 07:56:35 +0000612 Filter specific data.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000613
614
Christian Heimesfe337bf2008-03-23 21:54:12 +0000615.. attribute:: kevent.udata
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000616
Georg Brandl1b5ab452009-08-13 07:56:35 +0000617 User defined value.