blob: 5e079bb7ceb1e99c0129340362d127f2680486d0 [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 Brandl3dd57812008-03-22 20:08:43 +000053.. function:: kqueue(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 Brandl8ec7f652007-08-15 14:28:01 +000061.. function:: select(iwtd, owtd, ewtd[, timeout])
62
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
66 named :meth:`fileno` returning such an integer. The three sequences of
67 waitable objects are for input, output and 'exceptional conditions',
68 respectively. Empty sequences are allowed, but acceptance of three empty
69 sequences is platform-dependent. (It is known to work on Unix but not on
70 Windows.) The optional *timeout* argument specifies a time-out as a floating
71 point number in seconds. When the *timeout* argument is omitted the function
72 blocks until at least one file descriptor is ready. A time-out value of zero
73 specifies a poll and never blocks.
74
75 The return value is a triple of lists of objects that are ready: subsets of the
76 first three arguments. When the time-out is reached without a file descriptor
77 becoming ready, three empty lists are returned.
78
79 .. index::
80 single: socket() (in module socket)
81 single: popen() (in module os)
82
83 Among the acceptable object types in the sequences are Python file objects (e.g.
84 ``sys.stdin``, or objects returned by :func:`open` or :func:`os.popen`), socket
85 objects returned by :func:`socket.socket`. You may also define a :dfn:`wrapper`
86 class yourself, as long as it has an appropriate :meth:`fileno` method (that
87 really returns a file descriptor, not just a random integer).
88
Georg Brandl8ec7f652007-08-15 14:28:01 +000089 .. note::
90
91 .. index:: single: WinSock
92
93 File objects on Windows are not acceptable, but sockets are. On Windows, the
94 underlying :cfunc:`select` function is provided by the WinSock library, and does
95 not handle file descriptors that don't originate from WinSock.
96
97
Christian Heimes0e9ab5f2008-03-21 23:49:44 +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 +-----------------------+-----------------------------------------------+
Andrew M. Kuchling5b1070a2008-04-07 23:57:21 +0000116 | :const:`EPOLLERR` | Error condition happened on the assoc. fd |
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000117 +-----------------------+-----------------------------------------------+
Andrew M. Kuchling5b1070a2008-04-07 23:57:21 +0000118 | :const:`EPOLLHUP` | Hang up happened on the assoc. fd |
Christian Heimes0e9ab5f2008-03-21 23:49:44 +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 Brandl8ec7f652007-08-15 14:28:01 +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 Heimes0e9ab5f2008-03-21 23:49:44 +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
227 .. versionadded:: 2.6
228
229
Georg Brandl8ec7f652007-08-15 14:28:01 +0000230.. method:: poll.unregister(fd)
231
232 Remove a file descriptor being tracked by a polling object. Just like the
233 :meth:`register` method, *fd* can be an integer or an object with a
234 :meth:`fileno` method that returns an integer.
235
236 Attempting to remove a file descriptor that was never registered causes a
237 :exc:`KeyError` exception to be raised.
238
239
240.. method:: poll.poll([timeout])
241
242 Polls the set of registered file descriptors, and returns a possibly-empty list
243 containing ``(fd, event)`` 2-tuples for the descriptors that have events or
244 errors to report. *fd* is the file descriptor, and *event* is a bitmask with
245 bits set for the reported events for that descriptor --- :const:`POLLIN` for
246 waiting input, :const:`POLLOUT` to indicate that the descriptor can be written
247 to, and so forth. An empty list indicates that the call timed out and no file
248 descriptors had any events to report. If *timeout* is given, it specifies the
249 length of time in milliseconds which the system will wait for events before
250 returning. If *timeout* is omitted, negative, or :const:`None`, the call will
251 block until there is an event for this poll object.
252
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000253
254.. _kqueue-objects:
255
256Kqueue Objects
257--------------
258
259.. method:: kqueue.close()
260
261 Close the control file descriptor of the kqueue object.
262
263
264.. method:: kqueue.fileno()
265
266 Return the file descriptor number of the control fd.
267
268
269.. method:: epoll.fromfd(fd)
270
271 Create a kqueue object from a given file descriptor.
272
273
274.. method:: control(changelist, max_events=0[, timeout=None]) -> eventlist
275
276 Low level interface to kevent
277
278 - changelist must be an iterable of kevent object or None
279 - max_events must be 0 or a positive integer
280 - timeout in seconds (floats possible)
281
282
283.. _kevent-objects:
284
285Kevent Objects
286--------------
287
Georg Brandl3dd57812008-03-22 20:08:43 +0000288http://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000289
Georg Brandl3dd57812008-03-22 20:08:43 +0000290.. attribute:: kevent.ident
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000291
292 Value used to identify the event. The interpretation depends on the filter
293 but it's usually the file descriptor. In the constructor ident can either
294 be an int or an object with a fileno() function. kevent stores the integer
295 internally.
296
Georg Brandl3dd57812008-03-22 20:08:43 +0000297.. attribute:: kevent.filter
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000298
299 Name of the kernel filter
300
301 +---------------------------+---------------------------------------------+
302 | Constant | Meaning |
303 +===========================+=============================================+
304 | :const:`KQ_FILTER_READ` | Takes a descriptor and returns whenever |
305 | | there is data available to read |
306 +---------------------------+---------------------------------------------+
307 | :const:`KQ_FILTER_WRITE` | Takes a descriptor and returns whenever |
308 | | there is data available to read |
309 +---------------------------+---------------------------------------------+
310 | :const:`KQ_FILTER_AIO` | AIO requests |
311 +---------------------------+---------------------------------------------+
312 | :const:`KQ_FILTER_VNODE` | Returns when one or more of the requested |
313 | | events watched in *fflag* occurs |
314 +---------------------------+---------------------------------------------+
315 | :const:`KQ_FILTER_PROC` | Watch for events on a process id |
316 +---------------------------+---------------------------------------------+
317 | :const:`KQ_FILTER_NETDEV` | Watch for events on a network device |
318 | | [not available on Mac OS X] |
319 +---------------------------+---------------------------------------------+
320 | :const:`KQ_FILTER_SIGNAL` | Returns whenever the watched signal is |
321 | | delivered to the process |
322 +---------------------------+---------------------------------------------+
323 | :const:`KQ_FILTER_TIMER` | Establishes an arbitrary timer |
324 +---------------------------+---------------------------------------------+
325
Georg Brandl3dd57812008-03-22 20:08:43 +0000326.. attribute:: kevent.flags
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000327
328 Filter action
329
330 +---------------------------+---------------------------------------------+
331 | Constant | Meaning |
332 +===========================+=============================================+
333 | :const:`KQ_EV_ADD` | Adds or modifies an event |
334 +---------------------------+---------------------------------------------+
335 | :const:`KQ_EV_DELETE` | Removes an event from the queue |
336 +---------------------------+---------------------------------------------+
337 | :const:`KQ_EV_ENABLE` | Permitscontrol() to returns the event |
338 +---------------------------+---------------------------------------------+
339 | :const:`KQ_EV_DISABLE` | Disablesevent |
340 +---------------------------+---------------------------------------------+
Benjamin Peterson90f36732008-07-12 20:16:19 +0000341 | :const:`KQ_EV_ONESHOT` | Removes event after first occurrence |
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000342 +---------------------------+---------------------------------------------+
343 | :const:`KQ_EV_CLEAR` | Reset the state after an event is retrieved |
344 +---------------------------+---------------------------------------------+
345 | :const:`KQ_EV_SYSFLAGS` | internal event |
346 +---------------------------+---------------------------------------------+
347 | :const:`KQ_EV_FLAG1` | internal event |
348 +---------------------------+---------------------------------------------+
349 | :const:`KQ_EV_EOF` | Filter specific EOF condition |
350 +---------------------------+---------------------------------------------+
351 | :const:`KQ_EV_ERROR` | See return values |
352 +---------------------------+---------------------------------------------+
353
354
Georg Brandl3dd57812008-03-22 20:08:43 +0000355.. attribute:: kevent.fflags
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000356
357 Filter specific flags
358
359
Georg Brandl3dd57812008-03-22 20:08:43 +0000360 *:const:`KQ_FILTER_READ` and :const:`KQ_FILTER_WRITE` filter flags*
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000361
362 +----------------------------+--------------------------------------------+
363 | Constant | Meaning |
364 +============================+============================================+
365 | :const:`KQ_NOTE_LOWAT` | low water mark of a socket buffer |
366 +----------------------------+--------------------------------------------+
367
368
369 *:const:`KQ_FILTER_VNODE` filter flags*
370
371 +----------------------------+--------------------------------------------+
372 | Constant | Meaning |
373 +============================+============================================+
374 | :const:`KQ_NOTE_DELETE` | *unlink()* was called |
375 +----------------------------+--------------------------------------------+
Benjamin Peterson90f36732008-07-12 20:16:19 +0000376 | :const:`KQ_NOTE_WRITE` | a write occurred |
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000377 +----------------------------+--------------------------------------------+
378 | :const:`KQ_NOTE_EXTEND` | the file was extended |
379 +----------------------------+--------------------------------------------+
380 | :const:`KQ_NOTE_ATTRIB` | an attribute was changed |
381 +----------------------------+--------------------------------------------+
382 | :const:`KQ_NOTE_LINK` | the link count has changed |
383 +----------------------------+--------------------------------------------+
384 | :const:`KQ_NOTE_RENAME` | the file was renamed |
385 +----------------------------+--------------------------------------------+
386 | :const:`KQ_NOTE_REVOKE` | access to the file was revoked |
387 +----------------------------+--------------------------------------------+
388
389
390 *:const:`KQ_FILTER_PROC` filter flags*
391
392 +----------------------------+--------------------------------------------+
393 | Constant | Meaning |
394 +============================+============================================+
395 | :const:`KQ_NOTE_EXIT` | the process has exited |
396 +----------------------------+--------------------------------------------+
397 | :const:`KQ_NOTE_FORK` | the process has called *fork()* |
398 +----------------------------+--------------------------------------------+
399 | :const:`KQ_NOTE_EXEC` | the process has executed a new process |
400 +----------------------------+--------------------------------------------+
401 | :const:`KQ_NOTE_PCTRLMASK` | internal filter flag |
402 +----------------------------+--------------------------------------------+
403 | :const:`KQ_NOTE_PDATAMASK` | internal filter flag |
404 +----------------------------+--------------------------------------------+
405 | :const:`KQ_NOTE_TRACK` | follow a process across *fork()* |
406 +----------------------------+--------------------------------------------+
407 | :const:`KQ_NOTE_CHILD` | returned on the child process for |
408 | | *NOTE_TRACK* |
409 +----------------------------+--------------------------------------------+
410 | :const:`KQ_NOTE_TRACKERR` | unable to attach to a child |
411 +----------------------------+--------------------------------------------+
412
413 *:const:`KQ_FILTER_NETDEV` filter flags* [not available on Mac OS X]
414
415 +----------------------------+--------------------------------------------+
416 | Constant | Meaning |
417 +============================+============================================+
418 | :const:`KQ_NOTE_LINKUP` | link is up |
419 +----------------------------+--------------------------------------------+
420 | :const:`KQ_NOTE_LINKDOWN` | link is down |
421 +----------------------------+--------------------------------------------+
422 | :const:`KQ_NOTE_LINKINV` | link state is invalid |
423 +----------------------------+--------------------------------------------+
424
425
Georg Brandl3dd57812008-03-22 20:08:43 +0000426.. attribute:: kevent.data
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000427
428 Filter specific data
429
430
Georg Brandl3dd57812008-03-22 20:08:43 +0000431.. attribute:: kevent.udata
Christian Heimes0e9ab5f2008-03-21 23:49:44 +0000432
433 User defined value