blob: 102d3eacf1a03d8e71d804ff3beacdb23d314fe3 [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
Christian Heimesfe337bf2008-03-23 21:54:12 +000049.. function:: kqueue(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 Brandl116aa622007-08-15 14:28:22 +000055.. function:: select(iwtd, owtd, ewtd[, timeout])
56
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
60 named :meth:`fileno` returning such an integer. The three sequences of
61 waitable objects are for input, output and 'exceptional conditions',
62 respectively. Empty sequences are allowed, but acceptance of three empty
63 sequences is platform-dependent. (It is known to work on Unix but not on
64 Windows.) The optional *timeout* argument specifies a time-out as a floating
65 point number in seconds. When the *timeout* argument is omitted the function
66 blocks until at least one file descriptor is ready. A time-out value of zero
67 specifies a poll and never blocks.
68
69 The return value is a triple of lists of objects that are ready: subsets of the
70 first three arguments. When the time-out is reached without a file descriptor
71 becoming ready, three empty lists are returned.
72
73 .. index::
74 single: socket() (in module socket)
75 single: popen() (in module os)
76
77 Among the acceptable object types in the sequences are Python file objects (e.g.
78 ``sys.stdin``, or objects returned by :func:`open` or :func:`os.popen`), socket
79 objects returned by :func:`socket.socket`. You may also define a :dfn:`wrapper`
80 class yourself, as long as it has an appropriate :meth:`fileno` method (that
81 really returns a file descriptor, not just a random integer).
82
Georg Brandl116aa622007-08-15 14:28:22 +000083 .. note::
84
85 .. index:: single: WinSock
86
87 File objects on Windows are not acceptable, but sockets are. On Windows, the
88 underlying :cfunc:`select` function is provided by the WinSock library, and does
89 not handle file descriptors that don't originate from WinSock.
90
91
Christian Heimes4fbc72b2008-03-22 00:47:35 +000092.. _epoll-objects:
93
94Edge and Level Trigger Polling (epoll) Objects
95----------------------------------------------
96
97 http://linux.die.net/man/4/epoll
98
99 *eventmask*
100
101 +-----------------------+-----------------------------------------------+
102 | Constant | Meaning |
103 +=======================+===============================================+
104 | :const:`EPOLLIN` | Available for read |
105 +-----------------------+-----------------------------------------------+
106 | :const:`EPOLLOUT` | Available for write |
107 +-----------------------+-----------------------------------------------+
108 | :const:`EPOLLPRI` | Urgent data for read |
109 +-----------------------+-----------------------------------------------+
Christian Heimes5e696852008-04-09 08:37:03 +0000110 | :const:`EPOLLERR` | Error condition happened on the assoc. fd |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000111 +-----------------------+-----------------------------------------------+
Christian Heimes5e696852008-04-09 08:37:03 +0000112 | :const:`EPOLLHUP` | Hang up happened on the assoc. fd |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000113 +-----------------------+-----------------------------------------------+
114 | :const:`EPOLLET` | Set Edge Trigger behavior, the default is |
115 | | Level Trigger behavior |
116 +-----------------------+-----------------------------------------------+
117 | :const:`EPOLLONESHOT` | Set one-shot behavior. After one event is |
118 | | pulled out, the fd is internally disabled |
119 +-----------------------+-----------------------------------------------+
120 | :const:`EPOLLRDNORM` | ??? |
121 +-----------------------+-----------------------------------------------+
122 | :const:`EPOLLRDBAND` | ??? |
123 +-----------------------+-----------------------------------------------+
124 | :const:`EPOLLWRNORM` | ??? |
125 +-----------------------+-----------------------------------------------+
126 | :const:`EPOLLWRBAND` | ??? |
127 +-----------------------+-----------------------------------------------+
128 | :const:`EPOLLMSG` | ??? |
129 +-----------------------+-----------------------------------------------+
130
131
132.. method:: epoll.close()
133
134 Close the control file descriptor of the epoll object.
135
136
137.. method:: epoll.fileno()
138
139 Return the file descriptor number of the control fd.
140
141
142.. method:: epoll.fromfd(fd)
143
144 Create an epoll object from a given file descriptor.
145
146
147.. method:: epoll.register(fd[, eventmask])
148
149 Register a fd descriptor with the epoll object.
150
151
152.. method:: epoll.modify(fd, eventmask)
153
154 Modify a register file descriptor.
155
156
157.. method:: epoll.unregister(fd)
158
159 Remove a registered file descriptor from the epoll object.
160
161
162.. method:: epoll.poll([timeout=-1[, maxevents=-1]])
163
164 Wait for events. timeout in seconds (float)
165
166
Georg Brandl116aa622007-08-15 14:28:22 +0000167.. _poll-objects:
168
169Polling Objects
170---------------
171
172The :cfunc:`poll` system call, supported on most Unix systems, provides better
173scalability for network servers that service many, many clients at the same
174time. :cfunc:`poll` scales better because the system call only requires listing
175the file descriptors of interest, while :cfunc:`select` builds a bitmap, turns
176on bits for the fds of interest, and then afterward the whole bitmap has to be
177linearly scanned again. :cfunc:`select` is O(highest file descriptor), while
178:cfunc:`poll` is O(number of file descriptors).
179
180
181.. method:: poll.register(fd[, eventmask])
182
183 Register a file descriptor with the polling object. Future calls to the
184 :meth:`poll` method will then check whether the file descriptor has any pending
185 I/O events. *fd* can be either an integer, or an object with a :meth:`fileno`
186 method that returns an integer. File objects implement :meth:`fileno`, so they
187 can also be used as the argument.
188
189 *eventmask* is an optional bitmask describing the type of events you want to
190 check for, and can be a combination of the constants :const:`POLLIN`,
191 :const:`POLLPRI`, and :const:`POLLOUT`, described in the table below. If not
192 specified, the default value used will check for all 3 types of events.
193
194 +-------------------+------------------------------------------+
195 | Constant | Meaning |
196 +===================+==========================================+
197 | :const:`POLLIN` | There is data to read |
198 +-------------------+------------------------------------------+
199 | :const:`POLLPRI` | There is urgent data to read |
200 +-------------------+------------------------------------------+
201 | :const:`POLLOUT` | Ready for output: writing will not block |
202 +-------------------+------------------------------------------+
203 | :const:`POLLERR` | Error condition of some sort |
204 +-------------------+------------------------------------------+
205 | :const:`POLLHUP` | Hung up |
206 +-------------------+------------------------------------------+
207 | :const:`POLLNVAL` | Invalid request: descriptor not open |
208 +-------------------+------------------------------------------+
209
210 Registering a file descriptor that's already registered is not an error, and has
211 the same effect as registering the descriptor exactly once.
212
213
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000214.. method:: poll.modify(fd, eventmask)
215
216 Modifies an already registered fd. This has the same effect as
217 :meth:`register(fd, eventmask)`. Attempting to modify a file descriptor
218 that was never registered causes an :exc:`IOError` exception with errno
219 :const:`ENOENT` to be raised.
220
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000221
Georg Brandl116aa622007-08-15 14:28:22 +0000222.. method:: poll.unregister(fd)
223
224 Remove a file descriptor being tracked by a polling object. Just like the
225 :meth:`register` method, *fd* can be an integer or an object with a
226 :meth:`fileno` method that returns an integer.
227
228 Attempting to remove a file descriptor that was never registered causes a
229 :exc:`KeyError` exception to be raised.
230
231
232.. method:: poll.poll([timeout])
233
234 Polls the set of registered file descriptors, and returns a possibly-empty list
235 containing ``(fd, event)`` 2-tuples for the descriptors that have events or
236 errors to report. *fd* is the file descriptor, and *event* is a bitmask with
237 bits set for the reported events for that descriptor --- :const:`POLLIN` for
238 waiting input, :const:`POLLOUT` to indicate that the descriptor can be written
239 to, and so forth. An empty list indicates that the call timed out and no file
240 descriptors had any events to report. If *timeout* is given, it specifies the
241 length of time in milliseconds which the system will wait for events before
242 returning. If *timeout* is omitted, negative, or :const:`None`, the call will
243 block until there is an event for this poll object.
244
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000245
246.. _kqueue-objects:
247
248Kqueue Objects
249--------------
250
251.. method:: kqueue.close()
252
253 Close the control file descriptor of the kqueue object.
254
255
256.. method:: kqueue.fileno()
257
258 Return the file descriptor number of the control fd.
259
260
261.. method:: epoll.fromfd(fd)
262
263 Create a kqueue object from a given file descriptor.
264
265
266.. method:: control(changelist, max_events=0[, timeout=None]) -> eventlist
267
268 Low level interface to kevent
269
270 - changelist must be an iterable of kevent object or None
271 - max_events must be 0 or a positive integer
272 - timeout in seconds (floats possible)
273
274
275.. _kevent-objects:
276
277Kevent Objects
278--------------
279
Christian Heimesfe337bf2008-03-23 21:54:12 +0000280http://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000281
Christian Heimesfe337bf2008-03-23 21:54:12 +0000282.. attribute:: kevent.ident
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000283
284 Value used to identify the event. The interpretation depends on the filter
285 but it's usually the file descriptor. In the constructor ident can either
286 be an int or an object with a fileno() function. kevent stores the integer
287 internally.
288
Christian Heimesfe337bf2008-03-23 21:54:12 +0000289.. attribute:: kevent.filter
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000290
291 Name of the kernel filter
292
293 +---------------------------+---------------------------------------------+
294 | Constant | Meaning |
295 +===========================+=============================================+
296 | :const:`KQ_FILTER_READ` | Takes a descriptor and returns whenever |
297 | | there is data available to read |
298 +---------------------------+---------------------------------------------+
299 | :const:`KQ_FILTER_WRITE` | Takes a descriptor and returns whenever |
300 | | there is data available to read |
301 +---------------------------+---------------------------------------------+
302 | :const:`KQ_FILTER_AIO` | AIO requests |
303 +---------------------------+---------------------------------------------+
304 | :const:`KQ_FILTER_VNODE` | Returns when one or more of the requested |
305 | | events watched in *fflag* occurs |
306 +---------------------------+---------------------------------------------+
307 | :const:`KQ_FILTER_PROC` | Watch for events on a process id |
308 +---------------------------+---------------------------------------------+
309 | :const:`KQ_FILTER_NETDEV` | Watch for events on a network device |
310 | | [not available on Mac OS X] |
311 +---------------------------+---------------------------------------------+
312 | :const:`KQ_FILTER_SIGNAL` | Returns whenever the watched signal is |
313 | | delivered to the process |
314 +---------------------------+---------------------------------------------+
315 | :const:`KQ_FILTER_TIMER` | Establishes an arbitrary timer |
316 +---------------------------+---------------------------------------------+
317
Christian Heimesfe337bf2008-03-23 21:54:12 +0000318.. attribute:: kevent.flags
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000319
320 Filter action
321
322 +---------------------------+---------------------------------------------+
323 | Constant | Meaning |
324 +===========================+=============================================+
325 | :const:`KQ_EV_ADD` | Adds or modifies an event |
326 +---------------------------+---------------------------------------------+
327 | :const:`KQ_EV_DELETE` | Removes an event from the queue |
328 +---------------------------+---------------------------------------------+
329 | :const:`KQ_EV_ENABLE` | Permitscontrol() to returns the event |
330 +---------------------------+---------------------------------------------+
331 | :const:`KQ_EV_DISABLE` | Disablesevent |
332 +---------------------------+---------------------------------------------+
333 | :const:`KQ_EV_ONESHOT` | Removes event after first occurence |
334 +---------------------------+---------------------------------------------+
335 | :const:`KQ_EV_CLEAR` | Reset the state after an event is retrieved |
336 +---------------------------+---------------------------------------------+
337 | :const:`KQ_EV_SYSFLAGS` | internal event |
338 +---------------------------+---------------------------------------------+
339 | :const:`KQ_EV_FLAG1` | internal event |
340 +---------------------------+---------------------------------------------+
341 | :const:`KQ_EV_EOF` | Filter specific EOF condition |
342 +---------------------------+---------------------------------------------+
343 | :const:`KQ_EV_ERROR` | See return values |
344 +---------------------------+---------------------------------------------+
345
346
Christian Heimesfe337bf2008-03-23 21:54:12 +0000347.. attribute:: kevent.fflags
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000348
349 Filter specific flags
350
351
Christian Heimesfe337bf2008-03-23 21:54:12 +0000352 *:const:`KQ_FILTER_READ` and :const:`KQ_FILTER_WRITE` filter flags*
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000353
354 +----------------------------+--------------------------------------------+
355 | Constant | Meaning |
356 +============================+============================================+
357 | :const:`KQ_NOTE_LOWAT` | low water mark of a socket buffer |
358 +----------------------------+--------------------------------------------+
359
360
361 *:const:`KQ_FILTER_VNODE` filter flags*
362
363 +----------------------------+--------------------------------------------+
364 | Constant | Meaning |
365 +============================+============================================+
366 | :const:`KQ_NOTE_DELETE` | *unlink()* was called |
367 +----------------------------+--------------------------------------------+
368 | :const:`KQ_NOTE_WRITE` | a write occured |
369 +----------------------------+--------------------------------------------+
370 | :const:`KQ_NOTE_EXTEND` | the file was extended |
371 +----------------------------+--------------------------------------------+
372 | :const:`KQ_NOTE_ATTRIB` | an attribute was changed |
373 +----------------------------+--------------------------------------------+
374 | :const:`KQ_NOTE_LINK` | the link count has changed |
375 +----------------------------+--------------------------------------------+
376 | :const:`KQ_NOTE_RENAME` | the file was renamed |
377 +----------------------------+--------------------------------------------+
378 | :const:`KQ_NOTE_REVOKE` | access to the file was revoked |
379 +----------------------------+--------------------------------------------+
380
381
382 *:const:`KQ_FILTER_PROC` filter flags*
383
384 +----------------------------+--------------------------------------------+
385 | Constant | Meaning |
386 +============================+============================================+
387 | :const:`KQ_NOTE_EXIT` | the process has exited |
388 +----------------------------+--------------------------------------------+
389 | :const:`KQ_NOTE_FORK` | the process has called *fork()* |
390 +----------------------------+--------------------------------------------+
391 | :const:`KQ_NOTE_EXEC` | the process has executed a new process |
392 +----------------------------+--------------------------------------------+
393 | :const:`KQ_NOTE_PCTRLMASK` | internal filter flag |
394 +----------------------------+--------------------------------------------+
395 | :const:`KQ_NOTE_PDATAMASK` | internal filter flag |
396 +----------------------------+--------------------------------------------+
397 | :const:`KQ_NOTE_TRACK` | follow a process across *fork()* |
398 +----------------------------+--------------------------------------------+
399 | :const:`KQ_NOTE_CHILD` | returned on the child process for |
400 | | *NOTE_TRACK* |
401 +----------------------------+--------------------------------------------+
402 | :const:`KQ_NOTE_TRACKERR` | unable to attach to a child |
403 +----------------------------+--------------------------------------------+
404
405 *:const:`KQ_FILTER_NETDEV` filter flags* [not available on Mac OS X]
406
407 +----------------------------+--------------------------------------------+
408 | Constant | Meaning |
409 +============================+============================================+
410 | :const:`KQ_NOTE_LINKUP` | link is up |
411 +----------------------------+--------------------------------------------+
412 | :const:`KQ_NOTE_LINKDOWN` | link is down |
413 +----------------------------+--------------------------------------------+
414 | :const:`KQ_NOTE_LINKINV` | link state is invalid |
415 +----------------------------+--------------------------------------------+
416
417
Christian Heimesfe337bf2008-03-23 21:54:12 +0000418.. attribute:: kevent.data
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000419
420 Filter specific data
421
422
Christian Heimesfe337bf2008-03-23 21:54:12 +0000423.. attribute:: kevent.udata
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000424
425 User defined value