blob: 2b2c890c640e9d88ba5141e1536fac9be245bdad [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`select` --- Waiting for I/O completion
3============================================
4
5.. module:: select
6 :synopsis: Wait for I/O completion on multiple streams.
7
8
9This module provides access to the :cfunc:`select` and :cfunc:`poll` functions
Christian Heimes4fbc72b2008-03-22 00:47:35 +000010available in most operating systems, :cfunc:`epoll` available on Linux 2.5+ and
11:cfunc:`kqueue` available on most BSD.
12Note 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
17The module defines the following:
18
19
20.. exception:: error
21
22 The exception raised when an error occurs. The accompanying value is a pair
23 containing the numeric error code from :cdata:`errno` and the corresponding
24 string, as would be printed by the C function :cfunc:`perror`.
25
26
Christian Heimesfe337bf2008-03-23 21:54:12 +000027.. function:: epoll([sizehint=-1])
Christian Heimes4fbc72b2008-03-22 00:47:35 +000028
Christian Heimesfe337bf2008-03-23 21:54:12 +000029 (Only supported on Linux 2.5.44 and newer.) Returns an edge polling object,
30 which can be used as Edge or Level Triggered interface for I/O events; see
31 section :ref:`epoll-objects` below for the methods supported by epolling
32 objects.
Christian Heimes4fbc72b2008-03-22 00:47:35 +000033
Christian Heimes4fbc72b2008-03-22 00:47:35 +000034
Georg Brandl116aa622007-08-15 14:28:22 +000035.. function:: poll()
36
37 (Not supported by all operating systems.) Returns a polling object, which
38 supports registering and unregistering file descriptors, and then polling them
39 for I/O events; see section :ref:`poll-objects` below for the methods supported
40 by polling objects.
41
42
Christian Heimesfe337bf2008-03-23 21:54:12 +000043.. function:: kqueue()
Christian Heimes4fbc72b2008-03-22 00:47:35 +000044
Christian Heimesfe337bf2008-03-23 21:54:12 +000045 (Only supported on BSD.) Returns a kernel queue object object; see section
46 :ref:`kqueue-objects` below for the methods supported by kqueue objects.
Christian Heimes4fbc72b2008-03-22 00:47:35 +000047
Christian Heimes4fbc72b2008-03-22 00:47:35 +000048
Benjamin Peterson9bc93512008-09-22 22:10:59 +000049.. function:: kevent(ident, filter=KQ_FILTER_READ, flags=KQ_ADD, fflags=0, data=0, udata=0)
Christian Heimes4fbc72b2008-03-22 00:47:35 +000050
Christian Heimesfe337bf2008-03-23 21:54:12 +000051 (Only supported on BSD.) Returns a kernel event object object; see section
52 :ref:`kevent-objects` below for the methods supported by kqueue objects.
Christian Heimes4fbc72b2008-03-22 00:47:35 +000053
Christian Heimes4fbc72b2008-03-22 00:47:35 +000054
Georg Brandl734e2682008-08-12 08:18:18 +000055.. function:: select(rlist, wlist, xlist[, timeout])
Georg Brandl116aa622007-08-15 14:28:22 +000056
57 This is a straightforward interface to the Unix :cfunc:`select` system call.
58 The first three arguments are sequences of 'waitable objects': either
59 integers representing file descriptors or objects with a parameterless method
Georg Brandl734e2682008-08-12 08:18:18 +000060 named :meth:`fileno` returning such an integer:
61
62 * *rlist*: wait until ready for reading
63 * *wlist*: wait until ready for writing
64 * *xlist*: wait for an "exceptional condition" (see the manual page for what
65 your system considers such a condition)
66
67 Empty sequences are allowed, but acceptance of three empty sequences is
68 platform-dependent. (It is known to work on Unix but not on Windows.) The
69 optional *timeout* argument specifies a time-out as a floating point number
70 in seconds. When the *timeout* argument is omitted the function blocks until
71 at least one file descriptor is ready. A time-out value of zero specifies a
72 poll and never blocks.
Georg Brandl116aa622007-08-15 14:28:22 +000073
74 The return value is a triple of lists of objects that are ready: subsets of the
75 first three arguments. When the time-out is reached without a file descriptor
76 becoming ready, three empty lists are returned.
77
78 .. index::
79 single: socket() (in module socket)
80 single: popen() (in module os)
81
82 Among the acceptable object types in the sequences are Python file objects (e.g.
83 ``sys.stdin``, or objects returned by :func:`open` or :func:`os.popen`), socket
84 objects returned by :func:`socket.socket`. You may also define a :dfn:`wrapper`
85 class yourself, as long as it has an appropriate :meth:`fileno` method (that
86 really returns a file descriptor, not just a random integer).
87
Georg Brandl116aa622007-08-15 14:28:22 +000088 .. note::
89
90 .. index:: single: WinSock
91
Georg Brandl734e2682008-08-12 08:18:18 +000092 File objects on Windows are not acceptable, but sockets are. On Windows,
93 the underlying :cfunc:`select` function is provided by the WinSock
94 library, and does not handle file descriptors that don't originate from
95 WinSock.
Georg Brandl116aa622007-08-15 14:28:22 +000096
Gregory P. Smithb970b862009-07-04 02:28:47 +000097.. attribute:: select.PIPE_BUF
98
99 Files reported as ready for writing by :func:`select`, :func:`poll` or
100 similar interfaces in this module are guaranteed to not block on a write
101 of up to :const:`PIPE_BUF` bytes.
Amaury Forgeot d'Arcace31022009-07-09 22:44:11 +0000102 This value is guaranteed by POSIX to be at least 512. Availability: Unix.
Gregory P. Smithb970b862009-07-04 02:28:47 +0000103
104 .. versionadded:: 2.7
105
Georg Brandl116aa622007-08-15 14:28:22 +0000106
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000107.. _epoll-objects:
108
109Edge and Level Trigger Polling (epoll) Objects
110----------------------------------------------
111
112 http://linux.die.net/man/4/epoll
113
114 *eventmask*
115
116 +-----------------------+-----------------------------------------------+
117 | Constant | Meaning |
118 +=======================+===============================================+
119 | :const:`EPOLLIN` | Available for read |
120 +-----------------------+-----------------------------------------------+
121 | :const:`EPOLLOUT` | Available for write |
122 +-----------------------+-----------------------------------------------+
123 | :const:`EPOLLPRI` | Urgent data for read |
124 +-----------------------+-----------------------------------------------+
Christian Heimes5e696852008-04-09 08:37:03 +0000125 | :const:`EPOLLERR` | Error condition happened on the assoc. fd |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000126 +-----------------------+-----------------------------------------------+
Christian Heimes5e696852008-04-09 08:37:03 +0000127 | :const:`EPOLLHUP` | Hang up happened on the assoc. fd |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000128 +-----------------------+-----------------------------------------------+
129 | :const:`EPOLLET` | Set Edge Trigger behavior, the default is |
130 | | Level Trigger behavior |
131 +-----------------------+-----------------------------------------------+
132 | :const:`EPOLLONESHOT` | Set one-shot behavior. After one event is |
133 | | pulled out, the fd is internally disabled |
134 +-----------------------+-----------------------------------------------+
135 | :const:`EPOLLRDNORM` | ??? |
136 +-----------------------+-----------------------------------------------+
137 | :const:`EPOLLRDBAND` | ??? |
138 +-----------------------+-----------------------------------------------+
139 | :const:`EPOLLWRNORM` | ??? |
140 +-----------------------+-----------------------------------------------+
141 | :const:`EPOLLWRBAND` | ??? |
142 +-----------------------+-----------------------------------------------+
143 | :const:`EPOLLMSG` | ??? |
144 +-----------------------+-----------------------------------------------+
145
146
147.. method:: epoll.close()
148
149 Close the control file descriptor of the epoll object.
150
151
152.. method:: epoll.fileno()
153
154 Return the file descriptor number of the control fd.
155
156
157.. method:: epoll.fromfd(fd)
158
159 Create an epoll object from a given file descriptor.
160
161
162.. method:: epoll.register(fd[, eventmask])
163
164 Register a fd descriptor with the epoll object.
165
R. David Murray1855c212009-05-31 19:44:27 +0000166 .. note::
167
168 Registering a file descriptor that's already registered raises an
169 IOError -- contrary to :ref:`poll-objects`'s register.
170
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000171
172.. method:: epoll.modify(fd, eventmask)
173
174 Modify a register file descriptor.
175
176
177.. method:: epoll.unregister(fd)
178
179 Remove a registered file descriptor from the epoll object.
180
181
182.. method:: epoll.poll([timeout=-1[, maxevents=-1]])
183
184 Wait for events. timeout in seconds (float)
185
186
Georg Brandl116aa622007-08-15 14:28:22 +0000187.. _poll-objects:
188
189Polling Objects
190---------------
191
192The :cfunc:`poll` system call, supported on most Unix systems, provides better
193scalability for network servers that service many, many clients at the same
194time. :cfunc:`poll` scales better because the system call only requires listing
195the file descriptors of interest, while :cfunc:`select` builds a bitmap, turns
196on bits for the fds of interest, and then afterward the whole bitmap has to be
197linearly scanned again. :cfunc:`select` is O(highest file descriptor), while
198:cfunc:`poll` is O(number of file descriptors).
199
200
201.. method:: poll.register(fd[, eventmask])
202
203 Register a file descriptor with the polling object. Future calls to the
204 :meth:`poll` method will then check whether the file descriptor has any pending
205 I/O events. *fd* can be either an integer, or an object with a :meth:`fileno`
206 method that returns an integer. File objects implement :meth:`fileno`, so they
207 can also be used as the argument.
208
209 *eventmask* is an optional bitmask describing the type of events you want to
210 check for, and can be a combination of the constants :const:`POLLIN`,
211 :const:`POLLPRI`, and :const:`POLLOUT`, described in the table below. If not
212 specified, the default value used will check for all 3 types of events.
213
214 +-------------------+------------------------------------------+
215 | Constant | Meaning |
216 +===================+==========================================+
217 | :const:`POLLIN` | There is data to read |
218 +-------------------+------------------------------------------+
219 | :const:`POLLPRI` | There is urgent data to read |
220 +-------------------+------------------------------------------+
221 | :const:`POLLOUT` | Ready for output: writing will not block |
222 +-------------------+------------------------------------------+
223 | :const:`POLLERR` | Error condition of some sort |
224 +-------------------+------------------------------------------+
225 | :const:`POLLHUP` | Hung up |
226 +-------------------+------------------------------------------+
227 | :const:`POLLNVAL` | Invalid request: descriptor not open |
228 +-------------------+------------------------------------------+
229
230 Registering a file descriptor that's already registered is not an error, and has
231 the same effect as registering the descriptor exactly once.
232
233
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000234.. method:: poll.modify(fd, eventmask)
235
236 Modifies an already registered fd. This has the same effect as
237 :meth:`register(fd, eventmask)`. Attempting to modify a file descriptor
238 that was never registered causes an :exc:`IOError` exception with errno
239 :const:`ENOENT` to be raised.
240
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000241
Georg Brandl116aa622007-08-15 14:28:22 +0000242.. method:: poll.unregister(fd)
243
244 Remove a file descriptor being tracked by a polling object. Just like the
245 :meth:`register` method, *fd* can be an integer or an object with a
246 :meth:`fileno` method that returns an integer.
247
248 Attempting to remove a file descriptor that was never registered causes a
249 :exc:`KeyError` exception to be raised.
250
251
252.. method:: poll.poll([timeout])
253
254 Polls the set of registered file descriptors, and returns a possibly-empty list
255 containing ``(fd, event)`` 2-tuples for the descriptors that have events or
256 errors to report. *fd* is the file descriptor, and *event* is a bitmask with
257 bits set for the reported events for that descriptor --- :const:`POLLIN` for
258 waiting input, :const:`POLLOUT` to indicate that the descriptor can be written
259 to, and so forth. An empty list indicates that the call timed out and no file
260 descriptors had any events to report. If *timeout* is given, it specifies the
261 length of time in milliseconds which the system will wait for events before
262 returning. If *timeout* is omitted, negative, or :const:`None`, the call will
263 block until there is an event for this poll object.
264
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000265
266.. _kqueue-objects:
267
268Kqueue Objects
269--------------
270
271.. method:: kqueue.close()
272
273 Close the control file descriptor of the kqueue object.
274
275
276.. method:: kqueue.fileno()
277
278 Return the file descriptor number of the control fd.
279
280
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000281.. method:: kqueue.fromfd(fd)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000282
283 Create a kqueue object from a given file descriptor.
284
285
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000286.. method:: kqueue.control(changelist, max_events[, timeout=None]) -> eventlist
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000287
288 Low level interface to kevent
289
290 - changelist must be an iterable of kevent object or None
291 - max_events must be 0 or a positive integer
292 - timeout in seconds (floats possible)
293
294
295.. _kevent-objects:
296
297Kevent Objects
298--------------
299
Christian Heimesfe337bf2008-03-23 21:54:12 +0000300http://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000301
Christian Heimesfe337bf2008-03-23 21:54:12 +0000302.. attribute:: kevent.ident
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000303
304 Value used to identify the event. The interpretation depends on the filter
305 but it's usually the file descriptor. In the constructor ident can either
306 be an int or an object with a fileno() function. kevent stores the integer
307 internally.
308
Christian Heimesfe337bf2008-03-23 21:54:12 +0000309.. attribute:: kevent.filter
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000310
Georg Brandl1b5ab452009-08-13 07:56:35 +0000311 Name of the kernel filter.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000312
313 +---------------------------+---------------------------------------------+
314 | Constant | Meaning |
315 +===========================+=============================================+
316 | :const:`KQ_FILTER_READ` | Takes a descriptor and returns whenever |
317 | | there is data available to read |
318 +---------------------------+---------------------------------------------+
319 | :const:`KQ_FILTER_WRITE` | Takes a descriptor and returns whenever |
Georg Brandl1b5ab452009-08-13 07:56:35 +0000320 | | there is data available to write |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000321 +---------------------------+---------------------------------------------+
322 | :const:`KQ_FILTER_AIO` | AIO requests |
323 +---------------------------+---------------------------------------------+
324 | :const:`KQ_FILTER_VNODE` | Returns when one or more of the requested |
325 | | events watched in *fflag* occurs |
326 +---------------------------+---------------------------------------------+
327 | :const:`KQ_FILTER_PROC` | Watch for events on a process id |
328 +---------------------------+---------------------------------------------+
329 | :const:`KQ_FILTER_NETDEV` | Watch for events on a network device |
330 | | [not available on Mac OS X] |
331 +---------------------------+---------------------------------------------+
332 | :const:`KQ_FILTER_SIGNAL` | Returns whenever the watched signal is |
333 | | delivered to the process |
334 +---------------------------+---------------------------------------------+
335 | :const:`KQ_FILTER_TIMER` | Establishes an arbitrary timer |
336 +---------------------------+---------------------------------------------+
337
Christian Heimesfe337bf2008-03-23 21:54:12 +0000338.. attribute:: kevent.flags
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000339
Georg Brandl1b5ab452009-08-13 07:56:35 +0000340 Filter action.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000341
342 +---------------------------+---------------------------------------------+
343 | Constant | Meaning |
344 +===========================+=============================================+
345 | :const:`KQ_EV_ADD` | Adds or modifies an event |
346 +---------------------------+---------------------------------------------+
347 | :const:`KQ_EV_DELETE` | Removes an event from the queue |
348 +---------------------------+---------------------------------------------+
349 | :const:`KQ_EV_ENABLE` | Permitscontrol() to returns the event |
350 +---------------------------+---------------------------------------------+
351 | :const:`KQ_EV_DISABLE` | Disablesevent |
352 +---------------------------+---------------------------------------------+
Georg Brandl2ee470f2008-07-16 12:55:28 +0000353 | :const:`KQ_EV_ONESHOT` | Removes event after first occurrence |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000354 +---------------------------+---------------------------------------------+
355 | :const:`KQ_EV_CLEAR` | Reset the state after an event is retrieved |
356 +---------------------------+---------------------------------------------+
357 | :const:`KQ_EV_SYSFLAGS` | internal event |
358 +---------------------------+---------------------------------------------+
359 | :const:`KQ_EV_FLAG1` | internal event |
360 +---------------------------+---------------------------------------------+
361 | :const:`KQ_EV_EOF` | Filter specific EOF condition |
362 +---------------------------+---------------------------------------------+
363 | :const:`KQ_EV_ERROR` | See return values |
364 +---------------------------+---------------------------------------------+
365
366
Christian Heimesfe337bf2008-03-23 21:54:12 +0000367.. attribute:: kevent.fflags
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000368
Georg Brandl1b5ab452009-08-13 07:56:35 +0000369 Filter specific flags.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000370
Georg Brandl1b5ab452009-08-13 07:56:35 +0000371 :const:`KQ_FILTER_READ` and :const:`KQ_FILTER_WRITE` filter flags:
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000372
373 +----------------------------+--------------------------------------------+
374 | Constant | Meaning |
375 +============================+============================================+
376 | :const:`KQ_NOTE_LOWAT` | low water mark of a socket buffer |
377 +----------------------------+--------------------------------------------+
378
Georg Brandl1b5ab452009-08-13 07:56:35 +0000379 :const:`KQ_FILTER_VNODE` filter flags:
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000380
381 +----------------------------+--------------------------------------------+
382 | Constant | Meaning |
383 +============================+============================================+
384 | :const:`KQ_NOTE_DELETE` | *unlink()* was called |
385 +----------------------------+--------------------------------------------+
Georg Brandl2ee470f2008-07-16 12:55:28 +0000386 | :const:`KQ_NOTE_WRITE` | a write occurred |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000387 +----------------------------+--------------------------------------------+
388 | :const:`KQ_NOTE_EXTEND` | the file was extended |
389 +----------------------------+--------------------------------------------+
390 | :const:`KQ_NOTE_ATTRIB` | an attribute was changed |
391 +----------------------------+--------------------------------------------+
392 | :const:`KQ_NOTE_LINK` | the link count has changed |
393 +----------------------------+--------------------------------------------+
394 | :const:`KQ_NOTE_RENAME` | the file was renamed |
395 +----------------------------+--------------------------------------------+
396 | :const:`KQ_NOTE_REVOKE` | access to the file was revoked |
397 +----------------------------+--------------------------------------------+
398
Georg Brandl1b5ab452009-08-13 07:56:35 +0000399 :const:`KQ_FILTER_PROC` filter flags:
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000400
401 +----------------------------+--------------------------------------------+
402 | Constant | Meaning |
403 +============================+============================================+
404 | :const:`KQ_NOTE_EXIT` | the process has exited |
405 +----------------------------+--------------------------------------------+
406 | :const:`KQ_NOTE_FORK` | the process has called *fork()* |
407 +----------------------------+--------------------------------------------+
408 | :const:`KQ_NOTE_EXEC` | the process has executed a new process |
409 +----------------------------+--------------------------------------------+
410 | :const:`KQ_NOTE_PCTRLMASK` | internal filter flag |
411 +----------------------------+--------------------------------------------+
412 | :const:`KQ_NOTE_PDATAMASK` | internal filter flag |
413 +----------------------------+--------------------------------------------+
414 | :const:`KQ_NOTE_TRACK` | follow a process across *fork()* |
415 +----------------------------+--------------------------------------------+
416 | :const:`KQ_NOTE_CHILD` | returned on the child process for |
417 | | *NOTE_TRACK* |
418 +----------------------------+--------------------------------------------+
419 | :const:`KQ_NOTE_TRACKERR` | unable to attach to a child |
420 +----------------------------+--------------------------------------------+
421
Georg Brandl1b5ab452009-08-13 07:56:35 +0000422 :const:`KQ_FILTER_NETDEV` filter flags (not available on Mac OS X):
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000423
424 +----------------------------+--------------------------------------------+
425 | Constant | Meaning |
426 +============================+============================================+
427 | :const:`KQ_NOTE_LINKUP` | link is up |
428 +----------------------------+--------------------------------------------+
429 | :const:`KQ_NOTE_LINKDOWN` | link is down |
430 +----------------------------+--------------------------------------------+
431 | :const:`KQ_NOTE_LINKINV` | link state is invalid |
432 +----------------------------+--------------------------------------------+
433
434
Christian Heimesfe337bf2008-03-23 21:54:12 +0000435.. attribute:: kevent.data
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000436
Georg Brandl1b5ab452009-08-13 07:56:35 +0000437 Filter specific data.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000438
439
Christian Heimesfe337bf2008-03-23 21:54:12 +0000440.. attribute:: kevent.udata
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000441
Georg Brandl1b5ab452009-08-13 07:56:35 +0000442 User defined value.