blob: 351ecfa2be58e748eb97f1f2c38c8d72ad4308b1 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +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 Heimes0e9ab5f2008-03-21 23:49:44 +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 Brandl8ec7f652007-08-15 14:28:01 +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
Georg Brandl3dd57812008-03-22 20:08:43 +000027.. function:: epoll([sizehint=-1])
Christian Heimes0e9ab5f2008-03-21 23:49:44 +000028
Georg Brandl3dd57812008-03-22 20:08:43 +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 Heimes0e9ab5f2008-03-21 23:49:44 +000033
34 .. versionadded:: 2.6
35
36
Georg Brandl8ec7f652007-08-15 14:28:01 +000037.. function:: poll()
38
39 (Not supported by all operating systems.) Returns a polling object, which
40 supports registering and unregistering file descriptors, and then polling them
41 for I/O events; see section :ref:`poll-objects` below for the methods supported
42 by polling objects.
43
44
Georg Brandl3dd57812008-03-22 20:08:43 +000045.. function:: kqueue()
Christian Heimes0e9ab5f2008-03-21 23:49:44 +000046
Georg Brandl3dd57812008-03-22 20:08:43 +000047 (Only supported on BSD.) Returns a kernel queue object object; see section
48 :ref:`kqueue-objects` below for the methods supported by kqueue objects.
Christian Heimes0e9ab5f2008-03-21 23:49:44 +000049
50 .. versionadded:: 2.6
51
52
Georg Brandl2f3bd832008-09-21 07:14:44 +000053.. function:: kevent(ident, filter=KQ_FILTER_READ, flags=KQ_ADD, fflags=0, data=0, udata=0)
Christian Heimes0e9ab5f2008-03-21 23:49:44 +000054
Georg Brandl3dd57812008-03-22 20:08:43 +000055 (Only supported on BSD.) Returns a kernel event object object; see section
56 :ref:`kevent-objects` below for the methods supported by kqueue objects.
Christian Heimes0e9ab5f2008-03-21 23:49:44 +000057
58 .. versionadded:: 2.6
59
60
Georg Brandle7829a52008-08-04 07:31:50 +000061.. function:: select(rlist, wlist, xlist[, timeout])
Georg Brandl8ec7f652007-08-15 14:28:01 +000062
63 This is a straightforward interface to the Unix :cfunc:`select` system call.
64 The first three arguments are sequences of 'waitable objects': either
65 integers representing file descriptors or objects with a parameterless method
Georg Brandle7829a52008-08-04 07:31:50 +000066 named :meth:`fileno` returning such an integer:
67
68 * *rlist*: wait until ready for reading
69 * *wlist*: wait until ready for writing
70 * *xlist*: wait for an "exceptional condition" (see the manual page for what
71 your system considers such a condition)
72
73 Empty sequences are allowed, but acceptance of three empty sequences is
74 platform-dependent. (It is known to work on Unix but not on Windows.) The
75 optional *timeout* argument specifies a time-out as a floating point number
76 in seconds. When the *timeout* argument is omitted the function blocks until
77 at least one file descriptor is ready. A time-out value of zero specifies a
78 poll and never blocks.
Georg Brandl8ec7f652007-08-15 14:28:01 +000079
80 The return value is a triple of lists of objects that are ready: subsets of the
81 first three arguments. When the time-out is reached without a file descriptor
82 becoming ready, three empty lists are returned.
83
84 .. index::
85 single: socket() (in module socket)
86 single: popen() (in module os)
87
88 Among the acceptable object types in the sequences are Python file objects (e.g.
89 ``sys.stdin``, or objects returned by :func:`open` or :func:`os.popen`), socket
90 objects returned by :func:`socket.socket`. You may also define a :dfn:`wrapper`
91 class yourself, as long as it has an appropriate :meth:`fileno` method (that
92 really returns a file descriptor, not just a random integer).
93
Georg Brandl8ec7f652007-08-15 14:28:01 +000094 .. note::
95
96 .. index:: single: WinSock
97
Georg Brandle7829a52008-08-04 07:31:50 +000098 File objects on Windows are not acceptable, but sockets are. On Windows,
99 the underlying :cfunc:`select` function is provided by the WinSock
100 library, and does not handle file descriptors that don't originate from
101 WinSock.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000102
103
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000104.. _epoll-objects:
105
106Edge and Level Trigger Polling (epoll) Objects
107----------------------------------------------
108
109 http://linux.die.net/man/4/epoll
110
111 *eventmask*
112
113 +-----------------------+-----------------------------------------------+
114 | Constant | Meaning |
115 +=======================+===============================================+
116 | :const:`EPOLLIN` | Available for read |
117 +-----------------------+-----------------------------------------------+
118 | :const:`EPOLLOUT` | Available for write |
119 +-----------------------+-----------------------------------------------+
120 | :const:`EPOLLPRI` | Urgent data for read |
121 +-----------------------+-----------------------------------------------+
Andrew M. Kuchling5b1070a2008-04-07 23:57:21 +0000122 | :const:`EPOLLERR` | Error condition happened on the assoc. fd |
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000123 +-----------------------+-----------------------------------------------+
Andrew M. Kuchling5b1070a2008-04-07 23:57:21 +0000124 | :const:`EPOLLHUP` | Hang up happened on the assoc. fd |
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000125 +-----------------------+-----------------------------------------------+
126 | :const:`EPOLLET` | Set Edge Trigger behavior, the default is |
127 | | Level Trigger behavior |
128 +-----------------------+-----------------------------------------------+
129 | :const:`EPOLLONESHOT` | Set one-shot behavior. After one event is |
130 | | pulled out, the fd is internally disabled |
131 +-----------------------+-----------------------------------------------+
132 | :const:`EPOLLRDNORM` | ??? |
133 +-----------------------+-----------------------------------------------+
134 | :const:`EPOLLRDBAND` | ??? |
135 +-----------------------+-----------------------------------------------+
136 | :const:`EPOLLWRNORM` | ??? |
137 +-----------------------+-----------------------------------------------+
138 | :const:`EPOLLWRBAND` | ??? |
139 +-----------------------+-----------------------------------------------+
140 | :const:`EPOLLMSG` | ??? |
141 +-----------------------+-----------------------------------------------+
142
143
144.. method:: epoll.close()
145
146 Close the control file descriptor of the epoll object.
147
148
149.. method:: epoll.fileno()
150
151 Return the file descriptor number of the control fd.
152
153
154.. method:: epoll.fromfd(fd)
155
156 Create an epoll object from a given file descriptor.
157
158
159.. method:: epoll.register(fd[, eventmask])
160
161 Register a fd descriptor with the epoll object.
162
R. David Murraye6a4d782009-05-31 19:29:05 +0000163 .. note::
164
165 Registering a file descriptor that's already registered raises an
166 IOError -- contrary to :ref:`poll-objects`'s register.
167
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000168
169.. method:: epoll.modify(fd, eventmask)
170
171 Modify a register file descriptor.
172
173
174.. method:: epoll.unregister(fd)
175
176 Remove a registered file descriptor from the epoll object.
177
178
179.. method:: epoll.poll([timeout=-1[, maxevents=-1]])
180
181 Wait for events. timeout in seconds (float)
182
183
Georg Brandl8ec7f652007-08-15 14:28:01 +0000184.. _poll-objects:
185
186Polling Objects
187---------------
188
189The :cfunc:`poll` system call, supported on most Unix systems, provides better
190scalability for network servers that service many, many clients at the same
191time. :cfunc:`poll` scales better because the system call only requires listing
192the file descriptors of interest, while :cfunc:`select` builds a bitmap, turns
193on bits for the fds of interest, and then afterward the whole bitmap has to be
194linearly scanned again. :cfunc:`select` is O(highest file descriptor), while
195:cfunc:`poll` is O(number of file descriptors).
196
197
198.. method:: poll.register(fd[, eventmask])
199
200 Register a file descriptor with the polling object. Future calls to the
201 :meth:`poll` method will then check whether the file descriptor has any pending
202 I/O events. *fd* can be either an integer, or an object with a :meth:`fileno`
203 method that returns an integer. File objects implement :meth:`fileno`, so they
204 can also be used as the argument.
205
206 *eventmask* is an optional bitmask describing the type of events you want to
207 check for, and can be a combination of the constants :const:`POLLIN`,
208 :const:`POLLPRI`, and :const:`POLLOUT`, described in the table below. If not
209 specified, the default value used will check for all 3 types of events.
210
211 +-------------------+------------------------------------------+
212 | Constant | Meaning |
213 +===================+==========================================+
214 | :const:`POLLIN` | There is data to read |
215 +-------------------+------------------------------------------+
216 | :const:`POLLPRI` | There is urgent data to read |
217 +-------------------+------------------------------------------+
218 | :const:`POLLOUT` | Ready for output: writing will not block |
219 +-------------------+------------------------------------------+
220 | :const:`POLLERR` | Error condition of some sort |
221 +-------------------+------------------------------------------+
222 | :const:`POLLHUP` | Hung up |
223 +-------------------+------------------------------------------+
224 | :const:`POLLNVAL` | Invalid request: descriptor not open |
225 +-------------------+------------------------------------------+
226
227 Registering a file descriptor that's already registered is not an error, and has
228 the same effect as registering the descriptor exactly once.
229
230
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000231.. method:: poll.modify(fd, eventmask)
232
233 Modifies an already registered fd. This has the same effect as
234 :meth:`register(fd, eventmask)`. Attempting to modify a file descriptor
235 that was never registered causes an :exc:`IOError` exception with errno
236 :const:`ENOENT` to be raised.
237
238 .. versionadded:: 2.6
239
240
Georg Brandl8ec7f652007-08-15 14:28:01 +0000241.. method:: poll.unregister(fd)
242
243 Remove a file descriptor being tracked by a polling object. Just like the
244 :meth:`register` method, *fd* can be an integer or an object with a
245 :meth:`fileno` method that returns an integer.
246
247 Attempting to remove a file descriptor that was never registered causes a
248 :exc:`KeyError` exception to be raised.
249
250
251.. method:: poll.poll([timeout])
252
253 Polls the set of registered file descriptors, and returns a possibly-empty list
254 containing ``(fd, event)`` 2-tuples for the descriptors that have events or
255 errors to report. *fd* is the file descriptor, and *event* is a bitmask with
256 bits set for the reported events for that descriptor --- :const:`POLLIN` for
257 waiting input, :const:`POLLOUT` to indicate that the descriptor can be written
258 to, and so forth. An empty list indicates that the call timed out and no file
259 descriptors had any events to report. If *timeout* is given, it specifies the
260 length of time in milliseconds which the system will wait for events before
261 returning. If *timeout* is omitted, negative, or :const:`None`, the call will
262 block until there is an event for this poll object.
263
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000264
265.. _kqueue-objects:
266
267Kqueue Objects
268--------------
269
270.. method:: kqueue.close()
271
272 Close the control file descriptor of the kqueue object.
273
274
275.. method:: kqueue.fileno()
276
277 Return the file descriptor number of the control fd.
278
279
Georg Brandl2f3bd832008-09-21 07:14:44 +0000280.. method:: kqueue.fromfd(fd)
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000281
282 Create a kqueue object from a given file descriptor.
283
284
Georg Brandl2f3bd832008-09-21 07:14:44 +0000285.. method:: kqueue.control(changelist, max_events[, timeout=None]) -> eventlist
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000286
287 Low level interface to kevent
288
289 - changelist must be an iterable of kevent object or None
290 - max_events must be 0 or a positive integer
291 - timeout in seconds (floats possible)
292
293
294.. _kevent-objects:
295
296Kevent Objects
297--------------
298
Georg Brandl3dd57812008-03-22 20:08:43 +0000299http://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000300
Georg Brandl3dd57812008-03-22 20:08:43 +0000301.. attribute:: kevent.ident
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000302
303 Value used to identify the event. The interpretation depends on the filter
304 but it's usually the file descriptor. In the constructor ident can either
305 be an int or an object with a fileno() function. kevent stores the integer
306 internally.
307
Georg Brandl3dd57812008-03-22 20:08:43 +0000308.. attribute:: kevent.filter
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000309
310 Name of the kernel filter
311
312 +---------------------------+---------------------------------------------+
313 | Constant | Meaning |
314 +===========================+=============================================+
315 | :const:`KQ_FILTER_READ` | Takes a descriptor and returns whenever |
316 | | there is data available to read |
317 +---------------------------+---------------------------------------------+
318 | :const:`KQ_FILTER_WRITE` | Takes a descriptor and returns whenever |
319 | | there is data available to read |
320 +---------------------------+---------------------------------------------+
321 | :const:`KQ_FILTER_AIO` | AIO requests |
322 +---------------------------+---------------------------------------------+
323 | :const:`KQ_FILTER_VNODE` | Returns when one or more of the requested |
324 | | events watched in *fflag* occurs |
325 +---------------------------+---------------------------------------------+
326 | :const:`KQ_FILTER_PROC` | Watch for events on a process id |
327 +---------------------------+---------------------------------------------+
328 | :const:`KQ_FILTER_NETDEV` | Watch for events on a network device |
329 | | [not available on Mac OS X] |
330 +---------------------------+---------------------------------------------+
331 | :const:`KQ_FILTER_SIGNAL` | Returns whenever the watched signal is |
332 | | delivered to the process |
333 +---------------------------+---------------------------------------------+
334 | :const:`KQ_FILTER_TIMER` | Establishes an arbitrary timer |
335 +---------------------------+---------------------------------------------+
336
Georg Brandl3dd57812008-03-22 20:08:43 +0000337.. attribute:: kevent.flags
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000338
339 Filter action
340
341 +---------------------------+---------------------------------------------+
342 | Constant | Meaning |
343 +===========================+=============================================+
344 | :const:`KQ_EV_ADD` | Adds or modifies an event |
345 +---------------------------+---------------------------------------------+
346 | :const:`KQ_EV_DELETE` | Removes an event from the queue |
347 +---------------------------+---------------------------------------------+
348 | :const:`KQ_EV_ENABLE` | Permitscontrol() to returns the event |
349 +---------------------------+---------------------------------------------+
350 | :const:`KQ_EV_DISABLE` | Disablesevent |
351 +---------------------------+---------------------------------------------+
Benjamin Peterson90f36732008-07-12 20:16:19 +0000352 | :const:`KQ_EV_ONESHOT` | Removes event after first occurrence |
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000353 +---------------------------+---------------------------------------------+
354 | :const:`KQ_EV_CLEAR` | Reset the state after an event is retrieved |
355 +---------------------------+---------------------------------------------+
356 | :const:`KQ_EV_SYSFLAGS` | internal event |
357 +---------------------------+---------------------------------------------+
358 | :const:`KQ_EV_FLAG1` | internal event |
359 +---------------------------+---------------------------------------------+
360 | :const:`KQ_EV_EOF` | Filter specific EOF condition |
361 +---------------------------+---------------------------------------------+
362 | :const:`KQ_EV_ERROR` | See return values |
363 +---------------------------+---------------------------------------------+
364
365
Georg Brandl3dd57812008-03-22 20:08:43 +0000366.. attribute:: kevent.fflags
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000367
368 Filter specific flags
369
370
Georg Brandl4aef7032008-11-07 08:56:27 +0000371 :const:`KQ_FILTER_READ` and :const:`KQ_FILTER_WRITE` filter flags
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000372
373 +----------------------------+--------------------------------------------+
374 | Constant | Meaning |
375 +============================+============================================+
376 | :const:`KQ_NOTE_LOWAT` | low water mark of a socket buffer |
377 +----------------------------+--------------------------------------------+
378
379
Georg Brandl4aef7032008-11-07 08:56:27 +0000380 :const:`KQ_FILTER_VNODE` filter flags
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000381
382 +----------------------------+--------------------------------------------+
383 | Constant | Meaning |
384 +============================+============================================+
385 | :const:`KQ_NOTE_DELETE` | *unlink()* was called |
386 +----------------------------+--------------------------------------------+
Benjamin Peterson90f36732008-07-12 20:16:19 +0000387 | :const:`KQ_NOTE_WRITE` | a write occurred |
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000388 +----------------------------+--------------------------------------------+
389 | :const:`KQ_NOTE_EXTEND` | the file was extended |
390 +----------------------------+--------------------------------------------+
391 | :const:`KQ_NOTE_ATTRIB` | an attribute was changed |
392 +----------------------------+--------------------------------------------+
393 | :const:`KQ_NOTE_LINK` | the link count has changed |
394 +----------------------------+--------------------------------------------+
395 | :const:`KQ_NOTE_RENAME` | the file was renamed |
396 +----------------------------+--------------------------------------------+
397 | :const:`KQ_NOTE_REVOKE` | access to the file was revoked |
398 +----------------------------+--------------------------------------------+
399
400
Georg Brandl4aef7032008-11-07 08:56:27 +0000401 :const:`KQ_FILTER_PROC` filter flags
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000402
403 +----------------------------+--------------------------------------------+
404 | Constant | Meaning |
405 +============================+============================================+
406 | :const:`KQ_NOTE_EXIT` | the process has exited |
407 +----------------------------+--------------------------------------------+
408 | :const:`KQ_NOTE_FORK` | the process has called *fork()* |
409 +----------------------------+--------------------------------------------+
410 | :const:`KQ_NOTE_EXEC` | the process has executed a new process |
411 +----------------------------+--------------------------------------------+
412 | :const:`KQ_NOTE_PCTRLMASK` | internal filter flag |
413 +----------------------------+--------------------------------------------+
414 | :const:`KQ_NOTE_PDATAMASK` | internal filter flag |
415 +----------------------------+--------------------------------------------+
416 | :const:`KQ_NOTE_TRACK` | follow a process across *fork()* |
417 +----------------------------+--------------------------------------------+
418 | :const:`KQ_NOTE_CHILD` | returned on the child process for |
419 | | *NOTE_TRACK* |
420 +----------------------------+--------------------------------------------+
421 | :const:`KQ_NOTE_TRACKERR` | unable to attach to a child |
422 +----------------------------+--------------------------------------------+
423
Georg Brandl4aef7032008-11-07 08:56:27 +0000424 :const:`KQ_FILTER_NETDEV` filter flags [not available on Mac OS X]
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000425
426 +----------------------------+--------------------------------------------+
427 | Constant | Meaning |
428 +============================+============================================+
429 | :const:`KQ_NOTE_LINKUP` | link is up |
430 +----------------------------+--------------------------------------------+
431 | :const:`KQ_NOTE_LINKDOWN` | link is down |
432 +----------------------------+--------------------------------------------+
433 | :const:`KQ_NOTE_LINKINV` | link state is invalid |
434 +----------------------------+--------------------------------------------+
435
436
Georg Brandl3dd57812008-03-22 20:08:43 +0000437.. attribute:: kevent.data
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000438
439 Filter specific data
440
441
Georg Brandl3dd57812008-03-22 20:08:43 +0000442.. attribute:: kevent.udata
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000443
444 User defined value