blob: 48e289af7d2fd6bb9f61ec716fc01d4d1c67d3b7 [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
97
Christian Heimes4fbc72b2008-03-22 00:47:35 +000098.. _epoll-objects:
99
100Edge and Level Trigger Polling (epoll) Objects
101----------------------------------------------
102
103 http://linux.die.net/man/4/epoll
104
105 *eventmask*
106
107 +-----------------------+-----------------------------------------------+
108 | Constant | Meaning |
109 +=======================+===============================================+
110 | :const:`EPOLLIN` | Available for read |
111 +-----------------------+-----------------------------------------------+
112 | :const:`EPOLLOUT` | Available for write |
113 +-----------------------+-----------------------------------------------+
114 | :const:`EPOLLPRI` | Urgent data for read |
115 +-----------------------+-----------------------------------------------+
Christian Heimes5e696852008-04-09 08:37:03 +0000116 | :const:`EPOLLERR` | Error condition happened on the assoc. fd |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000117 +-----------------------+-----------------------------------------------+
Christian Heimes5e696852008-04-09 08:37:03 +0000118 | :const:`EPOLLHUP` | Hang up happened on the assoc. fd |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000119 +-----------------------+-----------------------------------------------+
120 | :const:`EPOLLET` | Set Edge Trigger behavior, the default is |
121 | | Level Trigger behavior |
122 +-----------------------+-----------------------------------------------+
123 | :const:`EPOLLONESHOT` | Set one-shot behavior. After one event is |
124 | | pulled out, the fd is internally disabled |
125 +-----------------------+-----------------------------------------------+
126 | :const:`EPOLLRDNORM` | ??? |
127 +-----------------------+-----------------------------------------------+
128 | :const:`EPOLLRDBAND` | ??? |
129 +-----------------------+-----------------------------------------------+
130 | :const:`EPOLLWRNORM` | ??? |
131 +-----------------------+-----------------------------------------------+
132 | :const:`EPOLLWRBAND` | ??? |
133 +-----------------------+-----------------------------------------------+
134 | :const:`EPOLLMSG` | ??? |
135 +-----------------------+-----------------------------------------------+
136
137
138.. method:: epoll.close()
139
140 Close the control file descriptor of the epoll object.
141
142
143.. method:: epoll.fileno()
144
145 Return the file descriptor number of the control fd.
146
147
148.. method:: epoll.fromfd(fd)
149
150 Create an epoll object from a given file descriptor.
151
152
153.. method:: epoll.register(fd[, eventmask])
154
155 Register a fd descriptor with the epoll object.
156
157
158.. method:: epoll.modify(fd, eventmask)
159
160 Modify a register file descriptor.
161
162
163.. method:: epoll.unregister(fd)
164
165 Remove a registered file descriptor from the epoll object.
166
167
168.. method:: epoll.poll([timeout=-1[, maxevents=-1]])
169
170 Wait for events. timeout in seconds (float)
171
172
Georg Brandl116aa622007-08-15 14:28:22 +0000173.. _poll-objects:
174
175Polling Objects
176---------------
177
178The :cfunc:`poll` system call, supported on most Unix systems, provides better
179scalability for network servers that service many, many clients at the same
180time. :cfunc:`poll` scales better because the system call only requires listing
181the file descriptors of interest, while :cfunc:`select` builds a bitmap, turns
182on bits for the fds of interest, and then afterward the whole bitmap has to be
183linearly scanned again. :cfunc:`select` is O(highest file descriptor), while
184:cfunc:`poll` is O(number of file descriptors).
185
186
187.. method:: poll.register(fd[, eventmask])
188
189 Register a file descriptor with the polling object. Future calls to the
190 :meth:`poll` method will then check whether the file descriptor has any pending
191 I/O events. *fd* can be either an integer, or an object with a :meth:`fileno`
192 method that returns an integer. File objects implement :meth:`fileno`, so they
193 can also be used as the argument.
194
195 *eventmask* is an optional bitmask describing the type of events you want to
196 check for, and can be a combination of the constants :const:`POLLIN`,
197 :const:`POLLPRI`, and :const:`POLLOUT`, described in the table below. If not
198 specified, the default value used will check for all 3 types of events.
199
200 +-------------------+------------------------------------------+
201 | Constant | Meaning |
202 +===================+==========================================+
203 | :const:`POLLIN` | There is data to read |
204 +-------------------+------------------------------------------+
205 | :const:`POLLPRI` | There is urgent data to read |
206 +-------------------+------------------------------------------+
207 | :const:`POLLOUT` | Ready for output: writing will not block |
208 +-------------------+------------------------------------------+
209 | :const:`POLLERR` | Error condition of some sort |
210 +-------------------+------------------------------------------+
211 | :const:`POLLHUP` | Hung up |
212 +-------------------+------------------------------------------+
213 | :const:`POLLNVAL` | Invalid request: descriptor not open |
214 +-------------------+------------------------------------------+
215
216 Registering a file descriptor that's already registered is not an error, and has
217 the same effect as registering the descriptor exactly once.
218
219
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000220.. method:: poll.modify(fd, eventmask)
221
222 Modifies an already registered fd. This has the same effect as
223 :meth:`register(fd, eventmask)`. Attempting to modify a file descriptor
224 that was never registered causes an :exc:`IOError` exception with errno
225 :const:`ENOENT` to be raised.
226
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000227
Georg Brandl116aa622007-08-15 14:28:22 +0000228.. method:: poll.unregister(fd)
229
230 Remove a file descriptor being tracked by a polling object. Just like the
231 :meth:`register` method, *fd* can be an integer or an object with a
232 :meth:`fileno` method that returns an integer.
233
234 Attempting to remove a file descriptor that was never registered causes a
235 :exc:`KeyError` exception to be raised.
236
237
238.. method:: poll.poll([timeout])
239
240 Polls the set of registered file descriptors, and returns a possibly-empty list
241 containing ``(fd, event)`` 2-tuples for the descriptors that have events or
242 errors to report. *fd* is the file descriptor, and *event* is a bitmask with
243 bits set for the reported events for that descriptor --- :const:`POLLIN` for
244 waiting input, :const:`POLLOUT` to indicate that the descriptor can be written
245 to, and so forth. An empty list indicates that the call timed out and no file
246 descriptors had any events to report. If *timeout* is given, it specifies the
247 length of time in milliseconds which the system will wait for events before
248 returning. If *timeout* is omitted, negative, or :const:`None`, the call will
249 block until there is an event for this poll object.
250
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000251
252.. _kqueue-objects:
253
254Kqueue Objects
255--------------
256
257.. method:: kqueue.close()
258
259 Close the control file descriptor of the kqueue object.
260
261
262.. method:: kqueue.fileno()
263
264 Return the file descriptor number of the control fd.
265
266
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000267.. method:: kqueue.fromfd(fd)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000268
269 Create a kqueue object from a given file descriptor.
270
271
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000272.. method:: kqueue.control(changelist, max_events[, timeout=None]) -> eventlist
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000273
274 Low level interface to kevent
275
276 - changelist must be an iterable of kevent object or None
277 - max_events must be 0 or a positive integer
278 - timeout in seconds (floats possible)
279
280
281.. _kevent-objects:
282
283Kevent Objects
284--------------
285
Christian Heimesfe337bf2008-03-23 21:54:12 +0000286http://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000287
Christian Heimesfe337bf2008-03-23 21:54:12 +0000288.. attribute:: kevent.ident
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000289
290 Value used to identify the event. The interpretation depends on the filter
291 but it's usually the file descriptor. In the constructor ident can either
292 be an int or an object with a fileno() function. kevent stores the integer
293 internally.
294
Christian Heimesfe337bf2008-03-23 21:54:12 +0000295.. attribute:: kevent.filter
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000296
297 Name of the kernel filter
298
299 +---------------------------+---------------------------------------------+
300 | Constant | Meaning |
301 +===========================+=============================================+
302 | :const:`KQ_FILTER_READ` | Takes a descriptor and returns whenever |
303 | | there is data available to read |
304 +---------------------------+---------------------------------------------+
305 | :const:`KQ_FILTER_WRITE` | Takes a descriptor and returns whenever |
306 | | there is data available to read |
307 +---------------------------+---------------------------------------------+
308 | :const:`KQ_FILTER_AIO` | AIO requests |
309 +---------------------------+---------------------------------------------+
310 | :const:`KQ_FILTER_VNODE` | Returns when one or more of the requested |
311 | | events watched in *fflag* occurs |
312 +---------------------------+---------------------------------------------+
313 | :const:`KQ_FILTER_PROC` | Watch for events on a process id |
314 +---------------------------+---------------------------------------------+
315 | :const:`KQ_FILTER_NETDEV` | Watch for events on a network device |
316 | | [not available on Mac OS X] |
317 +---------------------------+---------------------------------------------+
318 | :const:`KQ_FILTER_SIGNAL` | Returns whenever the watched signal is |
319 | | delivered to the process |
320 +---------------------------+---------------------------------------------+
321 | :const:`KQ_FILTER_TIMER` | Establishes an arbitrary timer |
322 +---------------------------+---------------------------------------------+
323
Christian Heimesfe337bf2008-03-23 21:54:12 +0000324.. attribute:: kevent.flags
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000325
326 Filter action
327
328 +---------------------------+---------------------------------------------+
329 | Constant | Meaning |
330 +===========================+=============================================+
331 | :const:`KQ_EV_ADD` | Adds or modifies an event |
332 +---------------------------+---------------------------------------------+
333 | :const:`KQ_EV_DELETE` | Removes an event from the queue |
334 +---------------------------+---------------------------------------------+
335 | :const:`KQ_EV_ENABLE` | Permitscontrol() to returns the event |
336 +---------------------------+---------------------------------------------+
337 | :const:`KQ_EV_DISABLE` | Disablesevent |
338 +---------------------------+---------------------------------------------+
Georg Brandl2ee470f2008-07-16 12:55:28 +0000339 | :const:`KQ_EV_ONESHOT` | Removes event after first occurrence |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000340 +---------------------------+---------------------------------------------+
341 | :const:`KQ_EV_CLEAR` | Reset the state after an event is retrieved |
342 +---------------------------+---------------------------------------------+
343 | :const:`KQ_EV_SYSFLAGS` | internal event |
344 +---------------------------+---------------------------------------------+
345 | :const:`KQ_EV_FLAG1` | internal event |
346 +---------------------------+---------------------------------------------+
347 | :const:`KQ_EV_EOF` | Filter specific EOF condition |
348 +---------------------------+---------------------------------------------+
349 | :const:`KQ_EV_ERROR` | See return values |
350 +---------------------------+---------------------------------------------+
351
352
Christian Heimesfe337bf2008-03-23 21:54:12 +0000353.. attribute:: kevent.fflags
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000354
355 Filter specific flags
356
357
Benjamin Peterson65676e42008-11-05 21:42:45 +0000358 :const:`KQ_FILTER_READ` and :const:`KQ_FILTER_WRITE` filter flags
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000359
360 +----------------------------+--------------------------------------------+
361 | Constant | Meaning |
362 +============================+============================================+
363 | :const:`KQ_NOTE_LOWAT` | low water mark of a socket buffer |
364 +----------------------------+--------------------------------------------+
365
366
Benjamin Peterson65676e42008-11-05 21:42:45 +0000367 :const:`KQ_FILTER_VNODE` filter flags
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000368
369 +----------------------------+--------------------------------------------+
370 | Constant | Meaning |
371 +============================+============================================+
372 | :const:`KQ_NOTE_DELETE` | *unlink()* was called |
373 +----------------------------+--------------------------------------------+
Georg Brandl2ee470f2008-07-16 12:55:28 +0000374 | :const:`KQ_NOTE_WRITE` | a write occurred |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000375 +----------------------------+--------------------------------------------+
376 | :const:`KQ_NOTE_EXTEND` | the file was extended |
377 +----------------------------+--------------------------------------------+
378 | :const:`KQ_NOTE_ATTRIB` | an attribute was changed |
379 +----------------------------+--------------------------------------------+
380 | :const:`KQ_NOTE_LINK` | the link count has changed |
381 +----------------------------+--------------------------------------------+
382 | :const:`KQ_NOTE_RENAME` | the file was renamed |
383 +----------------------------+--------------------------------------------+
384 | :const:`KQ_NOTE_REVOKE` | access to the file was revoked |
385 +----------------------------+--------------------------------------------+
386
387
Benjamin Peterson65676e42008-11-05 21:42:45 +0000388 :const:`KQ_FILTER_PROC` filter flags
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000389
390 +----------------------------+--------------------------------------------+
391 | Constant | Meaning |
392 +============================+============================================+
393 | :const:`KQ_NOTE_EXIT` | the process has exited |
394 +----------------------------+--------------------------------------------+
395 | :const:`KQ_NOTE_FORK` | the process has called *fork()* |
396 +----------------------------+--------------------------------------------+
397 | :const:`KQ_NOTE_EXEC` | the process has executed a new process |
398 +----------------------------+--------------------------------------------+
399 | :const:`KQ_NOTE_PCTRLMASK` | internal filter flag |
400 +----------------------------+--------------------------------------------+
401 | :const:`KQ_NOTE_PDATAMASK` | internal filter flag |
402 +----------------------------+--------------------------------------------+
403 | :const:`KQ_NOTE_TRACK` | follow a process across *fork()* |
404 +----------------------------+--------------------------------------------+
405 | :const:`KQ_NOTE_CHILD` | returned on the child process for |
406 | | *NOTE_TRACK* |
407 +----------------------------+--------------------------------------------+
408 | :const:`KQ_NOTE_TRACKERR` | unable to attach to a child |
409 +----------------------------+--------------------------------------------+
410
Benjamin Peterson65676e42008-11-05 21:42:45 +0000411 :const:`KQ_FILTER_NETDEV` filter flags [not available on Mac OS X]
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000412
413 +----------------------------+--------------------------------------------+
414 | Constant | Meaning |
415 +============================+============================================+
416 | :const:`KQ_NOTE_LINKUP` | link is up |
417 +----------------------------+--------------------------------------------+
418 | :const:`KQ_NOTE_LINKDOWN` | link is down |
419 +----------------------------+--------------------------------------------+
420 | :const:`KQ_NOTE_LINKINV` | link state is invalid |
421 +----------------------------+--------------------------------------------+
422
423
Christian Heimesfe337bf2008-03-23 21:54:12 +0000424.. attribute:: kevent.data
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000425
426 Filter specific data
427
428
Christian Heimesfe337bf2008-03-23 21:54:12 +0000429.. attribute:: kevent.udata
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000430
431 User defined value