blob: 70f73700b42e24e2d8113ae93f489d5e96334612 [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
Georg Brandl861b5222010-08-01 21:32:08 +000045 (Only supported on BSD.) Returns a kernel queue object; see section
Christian Heimesfe337bf2008-03-23 21:54:12 +000046 :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 Peterson68dbebc2009-12-31 03:30:26 +000049.. function:: kevent(ident, filter=KQ_FILTER_READ, flags=KQ_EV_ADD, fflags=0, data=0, udata=0)
Christian Heimes4fbc72b2008-03-22 00:47:35 +000050
Georg Brandl861b5222010-08-01 21:32:08 +000051 (Only supported on BSD.) Returns a kernel event object; see section
52 :ref:`kevent-objects` below for the methods supported by kevent 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
Antoine Pitrou25d535e2010-09-15 11:25:11 +000082 Among the acceptable object types in the sequences are Python :term:`file
83 objects <file object>` (e.g. ``sys.stdin``, or objects returned by
84 :func:`open` or :func:`os.popen`), socket objects returned by
85 :func:`socket.socket`. You may also define a :dfn:`wrapper` class yourself,
86 as long as it has an appropriate :meth:`fileno` method (that really returns
87 a file descriptor, not just a random integer).
Georg Brandl116aa622007-08-15 14:28:22 +000088
Georg Brandl116aa622007-08-15 14:28:22 +000089 .. note::
90
91 .. index:: single: WinSock
92
Georg Brandl734e2682008-08-12 08:18:18 +000093 File objects on Windows are not acceptable, but sockets are. On Windows,
94 the underlying :cfunc:`select` function is provided by the WinSock
95 library, and does not handle file descriptors that don't originate from
96 WinSock.
Georg Brandl116aa622007-08-15 14:28:22 +000097
98
Christian Heimes4fbc72b2008-03-22 00:47:35 +000099.. _epoll-objects:
100
101Edge and Level Trigger Polling (epoll) Objects
102----------------------------------------------
103
104 http://linux.die.net/man/4/epoll
105
106 *eventmask*
107
108 +-----------------------+-----------------------------------------------+
109 | Constant | Meaning |
110 +=======================+===============================================+
111 | :const:`EPOLLIN` | Available for read |
112 +-----------------------+-----------------------------------------------+
113 | :const:`EPOLLOUT` | Available for write |
114 +-----------------------+-----------------------------------------------+
115 | :const:`EPOLLPRI` | Urgent data for read |
116 +-----------------------+-----------------------------------------------+
Christian Heimes5e696852008-04-09 08:37:03 +0000117 | :const:`EPOLLERR` | Error condition happened on the assoc. fd |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000118 +-----------------------+-----------------------------------------------+
Christian Heimes5e696852008-04-09 08:37:03 +0000119 | :const:`EPOLLHUP` | Hang up happened on the assoc. fd |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000120 +-----------------------+-----------------------------------------------+
121 | :const:`EPOLLET` | Set Edge Trigger behavior, the default is |
122 | | Level Trigger behavior |
123 +-----------------------+-----------------------------------------------+
124 | :const:`EPOLLONESHOT` | Set one-shot behavior. After one event is |
125 | | pulled out, the fd is internally disabled |
126 +-----------------------+-----------------------------------------------+
127 | :const:`EPOLLRDNORM` | ??? |
128 +-----------------------+-----------------------------------------------+
129 | :const:`EPOLLRDBAND` | ??? |
130 +-----------------------+-----------------------------------------------+
131 | :const:`EPOLLWRNORM` | ??? |
132 +-----------------------+-----------------------------------------------+
133 | :const:`EPOLLWRBAND` | ??? |
134 +-----------------------+-----------------------------------------------+
135 | :const:`EPOLLMSG` | ??? |
136 +-----------------------+-----------------------------------------------+
137
138
139.. method:: epoll.close()
140
141 Close the control file descriptor of the epoll object.
142
143
144.. method:: epoll.fileno()
145
146 Return the file descriptor number of the control fd.
147
148
149.. method:: epoll.fromfd(fd)
150
151 Create an epoll object from a given file descriptor.
152
153
154.. method:: epoll.register(fd[, eventmask])
155
156 Register a fd descriptor with the epoll object.
157
R. David Murray1855c212009-05-31 19:44:27 +0000158 .. note::
159
160 Registering a file descriptor that's already registered raises an
161 IOError -- contrary to :ref:`poll-objects`'s register.
162
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000163
164.. method:: epoll.modify(fd, eventmask)
165
166 Modify a register file descriptor.
167
168
169.. method:: epoll.unregister(fd)
170
171 Remove a registered file descriptor from the epoll object.
172
173
174.. method:: epoll.poll([timeout=-1[, maxevents=-1]])
175
176 Wait for events. timeout in seconds (float)
177
178
Georg Brandl116aa622007-08-15 14:28:22 +0000179.. _poll-objects:
180
181Polling Objects
182---------------
183
184The :cfunc:`poll` system call, supported on most Unix systems, provides better
185scalability for network servers that service many, many clients at the same
186time. :cfunc:`poll` scales better because the system call only requires listing
187the file descriptors of interest, while :cfunc:`select` builds a bitmap, turns
188on bits for the fds of interest, and then afterward the whole bitmap has to be
189linearly scanned again. :cfunc:`select` is O(highest file descriptor), while
190:cfunc:`poll` is O(number of file descriptors).
191
192
193.. method:: poll.register(fd[, eventmask])
194
195 Register a file descriptor with the polling object. Future calls to the
196 :meth:`poll` method will then check whether the file descriptor has any pending
197 I/O events. *fd* can be either an integer, or an object with a :meth:`fileno`
198 method that returns an integer. File objects implement :meth:`fileno`, so they
199 can also be used as the argument.
200
201 *eventmask* is an optional bitmask describing the type of events you want to
202 check for, and can be a combination of the constants :const:`POLLIN`,
203 :const:`POLLPRI`, and :const:`POLLOUT`, described in the table below. If not
204 specified, the default value used will check for all 3 types of events.
205
206 +-------------------+------------------------------------------+
207 | Constant | Meaning |
208 +===================+==========================================+
209 | :const:`POLLIN` | There is data to read |
210 +-------------------+------------------------------------------+
211 | :const:`POLLPRI` | There is urgent data to read |
212 +-------------------+------------------------------------------+
213 | :const:`POLLOUT` | Ready for output: writing will not block |
214 +-------------------+------------------------------------------+
215 | :const:`POLLERR` | Error condition of some sort |
216 +-------------------+------------------------------------------+
217 | :const:`POLLHUP` | Hung up |
218 +-------------------+------------------------------------------+
219 | :const:`POLLNVAL` | Invalid request: descriptor not open |
220 +-------------------+------------------------------------------+
221
222 Registering a file descriptor that's already registered is not an error, and has
223 the same effect as registering the descriptor exactly once.
224
225
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000226.. method:: poll.modify(fd, eventmask)
227
228 Modifies an already registered fd. This has the same effect as
229 :meth:`register(fd, eventmask)`. Attempting to modify a file descriptor
230 that was never registered causes an :exc:`IOError` exception with errno
231 :const:`ENOENT` to be raised.
232
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000233
Georg Brandl116aa622007-08-15 14:28:22 +0000234.. method:: poll.unregister(fd)
235
236 Remove a file descriptor being tracked by a polling object. Just like the
237 :meth:`register` method, *fd* can be an integer or an object with a
238 :meth:`fileno` method that returns an integer.
239
240 Attempting to remove a file descriptor that was never registered causes a
241 :exc:`KeyError` exception to be raised.
242
243
244.. method:: poll.poll([timeout])
245
246 Polls the set of registered file descriptors, and returns a possibly-empty list
247 containing ``(fd, event)`` 2-tuples for the descriptors that have events or
248 errors to report. *fd* is the file descriptor, and *event* is a bitmask with
249 bits set for the reported events for that descriptor --- :const:`POLLIN` for
250 waiting input, :const:`POLLOUT` to indicate that the descriptor can be written
251 to, and so forth. An empty list indicates that the call timed out and no file
252 descriptors had any events to report. If *timeout* is given, it specifies the
253 length of time in milliseconds which the system will wait for events before
254 returning. If *timeout* is omitted, negative, or :const:`None`, the call will
255 block until there is an event for this poll object.
256
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000257
258.. _kqueue-objects:
259
260Kqueue Objects
261--------------
262
263.. method:: kqueue.close()
264
265 Close the control file descriptor of the kqueue object.
266
267
268.. method:: kqueue.fileno()
269
270 Return the file descriptor number of the control fd.
271
272
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000273.. method:: kqueue.fromfd(fd)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000274
275 Create a kqueue object from a given file descriptor.
276
277
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000278.. method:: kqueue.control(changelist, max_events[, timeout=None]) -> eventlist
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000279
280 Low level interface to kevent
281
282 - changelist must be an iterable of kevent object or None
283 - max_events must be 0 or a positive integer
284 - timeout in seconds (floats possible)
285
286
287.. _kevent-objects:
288
289Kevent Objects
290--------------
291
Christian Heimesfe337bf2008-03-23 21:54:12 +0000292http://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000293
Christian Heimesfe337bf2008-03-23 21:54:12 +0000294.. attribute:: kevent.ident
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000295
296 Value used to identify the event. The interpretation depends on the filter
297 but it's usually the file descriptor. In the constructor ident can either
298 be an int or an object with a fileno() function. kevent stores the integer
299 internally.
300
Christian Heimesfe337bf2008-03-23 21:54:12 +0000301.. attribute:: kevent.filter
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000302
Georg Brandlc5605df2009-08-13 08:26:44 +0000303 Name of the kernel filter.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000304
305 +---------------------------+---------------------------------------------+
306 | Constant | Meaning |
307 +===========================+=============================================+
308 | :const:`KQ_FILTER_READ` | Takes a descriptor and returns whenever |
309 | | there is data available to read |
310 +---------------------------+---------------------------------------------+
311 | :const:`KQ_FILTER_WRITE` | Takes a descriptor and returns whenever |
Georg Brandlc5605df2009-08-13 08:26:44 +0000312 | | there is data available to write |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000313 +---------------------------+---------------------------------------------+
314 | :const:`KQ_FILTER_AIO` | AIO requests |
315 +---------------------------+---------------------------------------------+
316 | :const:`KQ_FILTER_VNODE` | Returns when one or more of the requested |
317 | | events watched in *fflag* occurs |
318 +---------------------------+---------------------------------------------+
319 | :const:`KQ_FILTER_PROC` | Watch for events on a process id |
320 +---------------------------+---------------------------------------------+
321 | :const:`KQ_FILTER_NETDEV` | Watch for events on a network device |
322 | | [not available on Mac OS X] |
323 +---------------------------+---------------------------------------------+
324 | :const:`KQ_FILTER_SIGNAL` | Returns whenever the watched signal is |
325 | | delivered to the process |
326 +---------------------------+---------------------------------------------+
327 | :const:`KQ_FILTER_TIMER` | Establishes an arbitrary timer |
328 +---------------------------+---------------------------------------------+
329
Christian Heimesfe337bf2008-03-23 21:54:12 +0000330.. attribute:: kevent.flags
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000331
Georg Brandlc5605df2009-08-13 08:26:44 +0000332 Filter action.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000333
334 +---------------------------+---------------------------------------------+
335 | Constant | Meaning |
336 +===========================+=============================================+
337 | :const:`KQ_EV_ADD` | Adds or modifies an event |
338 +---------------------------+---------------------------------------------+
339 | :const:`KQ_EV_DELETE` | Removes an event from the queue |
340 +---------------------------+---------------------------------------------+
341 | :const:`KQ_EV_ENABLE` | Permitscontrol() to returns the event |
342 +---------------------------+---------------------------------------------+
343 | :const:`KQ_EV_DISABLE` | Disablesevent |
344 +---------------------------+---------------------------------------------+
Georg Brandl2ee470f2008-07-16 12:55:28 +0000345 | :const:`KQ_EV_ONESHOT` | Removes event after first occurrence |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000346 +---------------------------+---------------------------------------------+
347 | :const:`KQ_EV_CLEAR` | Reset the state after an event is retrieved |
348 +---------------------------+---------------------------------------------+
349 | :const:`KQ_EV_SYSFLAGS` | internal event |
350 +---------------------------+---------------------------------------------+
351 | :const:`KQ_EV_FLAG1` | internal event |
352 +---------------------------+---------------------------------------------+
353 | :const:`KQ_EV_EOF` | Filter specific EOF condition |
354 +---------------------------+---------------------------------------------+
355 | :const:`KQ_EV_ERROR` | See return values |
356 +---------------------------+---------------------------------------------+
357
358
Christian Heimesfe337bf2008-03-23 21:54:12 +0000359.. attribute:: kevent.fflags
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000360
Georg Brandlc5605df2009-08-13 08:26:44 +0000361 Filter specific flags.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000362
Georg Brandlc5605df2009-08-13 08:26:44 +0000363 :const:`KQ_FILTER_READ` and :const:`KQ_FILTER_WRITE` filter flags:
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000364
365 +----------------------------+--------------------------------------------+
366 | Constant | Meaning |
367 +============================+============================================+
368 | :const:`KQ_NOTE_LOWAT` | low water mark of a socket buffer |
369 +----------------------------+--------------------------------------------+
370
Georg Brandlc5605df2009-08-13 08:26:44 +0000371 :const:`KQ_FILTER_VNODE` filter flags:
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000372
373 +----------------------------+--------------------------------------------+
374 | Constant | Meaning |
375 +============================+============================================+
376 | :const:`KQ_NOTE_DELETE` | *unlink()* was called |
377 +----------------------------+--------------------------------------------+
Georg Brandl2ee470f2008-07-16 12:55:28 +0000378 | :const:`KQ_NOTE_WRITE` | a write occurred |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000379 +----------------------------+--------------------------------------------+
380 | :const:`KQ_NOTE_EXTEND` | the file was extended |
381 +----------------------------+--------------------------------------------+
382 | :const:`KQ_NOTE_ATTRIB` | an attribute was changed |
383 +----------------------------+--------------------------------------------+
384 | :const:`KQ_NOTE_LINK` | the link count has changed |
385 +----------------------------+--------------------------------------------+
386 | :const:`KQ_NOTE_RENAME` | the file was renamed |
387 +----------------------------+--------------------------------------------+
388 | :const:`KQ_NOTE_REVOKE` | access to the file was revoked |
389 +----------------------------+--------------------------------------------+
390
Georg Brandlc5605df2009-08-13 08:26:44 +0000391 :const:`KQ_FILTER_PROC` filter flags:
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000392
393 +----------------------------+--------------------------------------------+
394 | Constant | Meaning |
395 +============================+============================================+
396 | :const:`KQ_NOTE_EXIT` | the process has exited |
397 +----------------------------+--------------------------------------------+
398 | :const:`KQ_NOTE_FORK` | the process has called *fork()* |
399 +----------------------------+--------------------------------------------+
400 | :const:`KQ_NOTE_EXEC` | the process has executed a new process |
401 +----------------------------+--------------------------------------------+
402 | :const:`KQ_NOTE_PCTRLMASK` | internal filter flag |
403 +----------------------------+--------------------------------------------+
404 | :const:`KQ_NOTE_PDATAMASK` | internal filter flag |
405 +----------------------------+--------------------------------------------+
406 | :const:`KQ_NOTE_TRACK` | follow a process across *fork()* |
407 +----------------------------+--------------------------------------------+
408 | :const:`KQ_NOTE_CHILD` | returned on the child process for |
409 | | *NOTE_TRACK* |
410 +----------------------------+--------------------------------------------+
411 | :const:`KQ_NOTE_TRACKERR` | unable to attach to a child |
412 +----------------------------+--------------------------------------------+
413
Georg Brandlc5605df2009-08-13 08:26:44 +0000414 :const:`KQ_FILTER_NETDEV` filter flags (not available on Mac OS X):
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000415
416 +----------------------------+--------------------------------------------+
417 | Constant | Meaning |
418 +============================+============================================+
419 | :const:`KQ_NOTE_LINKUP` | link is up |
420 +----------------------------+--------------------------------------------+
421 | :const:`KQ_NOTE_LINKDOWN` | link is down |
422 +----------------------------+--------------------------------------------+
423 | :const:`KQ_NOTE_LINKINV` | link state is invalid |
424 +----------------------------+--------------------------------------------+
425
426
Christian Heimesfe337bf2008-03-23 21:54:12 +0000427.. attribute:: kevent.data
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000428
Georg Brandlc5605df2009-08-13 08:26:44 +0000429 Filter specific data.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000430
431
Christian Heimesfe337bf2008-03-23 21:54:12 +0000432.. attribute:: kevent.udata
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000433
Georg Brandlc5605df2009-08-13 08:26:44 +0000434 User defined value.