blob: 73e8cd9ea5a4b2652300f8177aab930a9b26a010 [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
R. David Murray1855c212009-05-31 19:44:27 +0000157 .. note::
158
159 Registering a file descriptor that's already registered raises an
160 IOError -- contrary to :ref:`poll-objects`'s register.
161
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000162
163.. method:: epoll.modify(fd, eventmask)
164
165 Modify a register file descriptor.
166
167
168.. method:: epoll.unregister(fd)
169
170 Remove a registered file descriptor from the epoll object.
171
172
173.. method:: epoll.poll([timeout=-1[, maxevents=-1]])
174
175 Wait for events. timeout in seconds (float)
176
177
Georg Brandl116aa622007-08-15 14:28:22 +0000178.. _poll-objects:
179
180Polling Objects
181---------------
182
183The :cfunc:`poll` system call, supported on most Unix systems, provides better
184scalability for network servers that service many, many clients at the same
185time. :cfunc:`poll` scales better because the system call only requires listing
186the file descriptors of interest, while :cfunc:`select` builds a bitmap, turns
187on bits for the fds of interest, and then afterward the whole bitmap has to be
188linearly scanned again. :cfunc:`select` is O(highest file descriptor), while
189:cfunc:`poll` is O(number of file descriptors).
190
191
192.. method:: poll.register(fd[, eventmask])
193
194 Register a file descriptor with the polling object. Future calls to the
195 :meth:`poll` method will then check whether the file descriptor has any pending
196 I/O events. *fd* can be either an integer, or an object with a :meth:`fileno`
197 method that returns an integer. File objects implement :meth:`fileno`, so they
198 can also be used as the argument.
199
200 *eventmask* is an optional bitmask describing the type of events you want to
201 check for, and can be a combination of the constants :const:`POLLIN`,
202 :const:`POLLPRI`, and :const:`POLLOUT`, described in the table below. If not
203 specified, the default value used will check for all 3 types of events.
204
205 +-------------------+------------------------------------------+
206 | Constant | Meaning |
207 +===================+==========================================+
208 | :const:`POLLIN` | There is data to read |
209 +-------------------+------------------------------------------+
210 | :const:`POLLPRI` | There is urgent data to read |
211 +-------------------+------------------------------------------+
212 | :const:`POLLOUT` | Ready for output: writing will not block |
213 +-------------------+------------------------------------------+
214 | :const:`POLLERR` | Error condition of some sort |
215 +-------------------+------------------------------------------+
216 | :const:`POLLHUP` | Hung up |
217 +-------------------+------------------------------------------+
218 | :const:`POLLNVAL` | Invalid request: descriptor not open |
219 +-------------------+------------------------------------------+
220
221 Registering a file descriptor that's already registered is not an error, and has
222 the same effect as registering the descriptor exactly once.
223
224
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000225.. method:: poll.modify(fd, eventmask)
226
227 Modifies an already registered fd. This has the same effect as
228 :meth:`register(fd, eventmask)`. Attempting to modify a file descriptor
229 that was never registered causes an :exc:`IOError` exception with errno
230 :const:`ENOENT` to be raised.
231
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000232
Georg Brandl116aa622007-08-15 14:28:22 +0000233.. method:: poll.unregister(fd)
234
235 Remove a file descriptor being tracked by a polling object. Just like the
236 :meth:`register` method, *fd* can be an integer or an object with a
237 :meth:`fileno` method that returns an integer.
238
239 Attempting to remove a file descriptor that was never registered causes a
240 :exc:`KeyError` exception to be raised.
241
242
243.. method:: poll.poll([timeout])
244
245 Polls the set of registered file descriptors, and returns a possibly-empty list
246 containing ``(fd, event)`` 2-tuples for the descriptors that have events or
247 errors to report. *fd* is the file descriptor, and *event* is a bitmask with
248 bits set for the reported events for that descriptor --- :const:`POLLIN` for
249 waiting input, :const:`POLLOUT` to indicate that the descriptor can be written
250 to, and so forth. An empty list indicates that the call timed out and no file
251 descriptors had any events to report. If *timeout* is given, it specifies the
252 length of time in milliseconds which the system will wait for events before
253 returning. If *timeout* is omitted, negative, or :const:`None`, the call will
254 block until there is an event for this poll object.
255
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000256
257.. _kqueue-objects:
258
259Kqueue Objects
260--------------
261
262.. method:: kqueue.close()
263
264 Close the control file descriptor of the kqueue object.
265
266
267.. method:: kqueue.fileno()
268
269 Return the file descriptor number of the control fd.
270
271
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000272.. method:: kqueue.fromfd(fd)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000273
274 Create a kqueue object from a given file descriptor.
275
276
Benjamin Peterson9bc93512008-09-22 22:10:59 +0000277.. method:: kqueue.control(changelist, max_events[, timeout=None]) -> eventlist
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000278
279 Low level interface to kevent
280
281 - changelist must be an iterable of kevent object or None
282 - max_events must be 0 or a positive integer
283 - timeout in seconds (floats possible)
284
285
286.. _kevent-objects:
287
288Kevent Objects
289--------------
290
Christian Heimesfe337bf2008-03-23 21:54:12 +0000291http://www.freebsd.org/cgi/man.cgi?query=kqueue&sektion=2
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000292
Christian Heimesfe337bf2008-03-23 21:54:12 +0000293.. attribute:: kevent.ident
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000294
295 Value used to identify the event. The interpretation depends on the filter
296 but it's usually the file descriptor. In the constructor ident can either
297 be an int or an object with a fileno() function. kevent stores the integer
298 internally.
299
Christian Heimesfe337bf2008-03-23 21:54:12 +0000300.. attribute:: kevent.filter
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000301
Georg Brandlc5605df2009-08-13 08:26:44 +0000302 Name of the kernel filter.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000303
304 +---------------------------+---------------------------------------------+
305 | Constant | Meaning |
306 +===========================+=============================================+
307 | :const:`KQ_FILTER_READ` | Takes a descriptor and returns whenever |
308 | | there is data available to read |
309 +---------------------------+---------------------------------------------+
310 | :const:`KQ_FILTER_WRITE` | Takes a descriptor and returns whenever |
Georg Brandlc5605df2009-08-13 08:26:44 +0000311 | | there is data available to write |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000312 +---------------------------+---------------------------------------------+
313 | :const:`KQ_FILTER_AIO` | AIO requests |
314 +---------------------------+---------------------------------------------+
315 | :const:`KQ_FILTER_VNODE` | Returns when one or more of the requested |
316 | | events watched in *fflag* occurs |
317 +---------------------------+---------------------------------------------+
318 | :const:`KQ_FILTER_PROC` | Watch for events on a process id |
319 +---------------------------+---------------------------------------------+
320 | :const:`KQ_FILTER_NETDEV` | Watch for events on a network device |
321 | | [not available on Mac OS X] |
322 +---------------------------+---------------------------------------------+
323 | :const:`KQ_FILTER_SIGNAL` | Returns whenever the watched signal is |
324 | | delivered to the process |
325 +---------------------------+---------------------------------------------+
326 | :const:`KQ_FILTER_TIMER` | Establishes an arbitrary timer |
327 +---------------------------+---------------------------------------------+
328
Christian Heimesfe337bf2008-03-23 21:54:12 +0000329.. attribute:: kevent.flags
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000330
Georg Brandlc5605df2009-08-13 08:26:44 +0000331 Filter action.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000332
333 +---------------------------+---------------------------------------------+
334 | Constant | Meaning |
335 +===========================+=============================================+
336 | :const:`KQ_EV_ADD` | Adds or modifies an event |
337 +---------------------------+---------------------------------------------+
338 | :const:`KQ_EV_DELETE` | Removes an event from the queue |
339 +---------------------------+---------------------------------------------+
340 | :const:`KQ_EV_ENABLE` | Permitscontrol() to returns the event |
341 +---------------------------+---------------------------------------------+
342 | :const:`KQ_EV_DISABLE` | Disablesevent |
343 +---------------------------+---------------------------------------------+
Georg Brandl2ee470f2008-07-16 12:55:28 +0000344 | :const:`KQ_EV_ONESHOT` | Removes event after first occurrence |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000345 +---------------------------+---------------------------------------------+
346 | :const:`KQ_EV_CLEAR` | Reset the state after an event is retrieved |
347 +---------------------------+---------------------------------------------+
348 | :const:`KQ_EV_SYSFLAGS` | internal event |
349 +---------------------------+---------------------------------------------+
350 | :const:`KQ_EV_FLAG1` | internal event |
351 +---------------------------+---------------------------------------------+
352 | :const:`KQ_EV_EOF` | Filter specific EOF condition |
353 +---------------------------+---------------------------------------------+
354 | :const:`KQ_EV_ERROR` | See return values |
355 +---------------------------+---------------------------------------------+
356
357
Christian Heimesfe337bf2008-03-23 21:54:12 +0000358.. attribute:: kevent.fflags
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000359
Georg Brandlc5605df2009-08-13 08:26:44 +0000360 Filter specific flags.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000361
Georg Brandlc5605df2009-08-13 08:26:44 +0000362 :const:`KQ_FILTER_READ` and :const:`KQ_FILTER_WRITE` filter flags:
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000363
364 +----------------------------+--------------------------------------------+
365 | Constant | Meaning |
366 +============================+============================================+
367 | :const:`KQ_NOTE_LOWAT` | low water mark of a socket buffer |
368 +----------------------------+--------------------------------------------+
369
Georg Brandlc5605df2009-08-13 08:26:44 +0000370 :const:`KQ_FILTER_VNODE` filter flags:
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000371
372 +----------------------------+--------------------------------------------+
373 | Constant | Meaning |
374 +============================+============================================+
375 | :const:`KQ_NOTE_DELETE` | *unlink()* was called |
376 +----------------------------+--------------------------------------------+
Georg Brandl2ee470f2008-07-16 12:55:28 +0000377 | :const:`KQ_NOTE_WRITE` | a write occurred |
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000378 +----------------------------+--------------------------------------------+
379 | :const:`KQ_NOTE_EXTEND` | the file was extended |
380 +----------------------------+--------------------------------------------+
381 | :const:`KQ_NOTE_ATTRIB` | an attribute was changed |
382 +----------------------------+--------------------------------------------+
383 | :const:`KQ_NOTE_LINK` | the link count has changed |
384 +----------------------------+--------------------------------------------+
385 | :const:`KQ_NOTE_RENAME` | the file was renamed |
386 +----------------------------+--------------------------------------------+
387 | :const:`KQ_NOTE_REVOKE` | access to the file was revoked |
388 +----------------------------+--------------------------------------------+
389
Georg Brandlc5605df2009-08-13 08:26:44 +0000390 :const:`KQ_FILTER_PROC` filter flags:
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000391
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
Georg Brandlc5605df2009-08-13 08:26:44 +0000413 :const:`KQ_FILTER_NETDEV` filter flags (not available on Mac OS X):
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000414
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
Christian Heimesfe337bf2008-03-23 21:54:12 +0000426.. attribute:: kevent.data
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000427
Georg Brandlc5605df2009-08-13 08:26:44 +0000428 Filter specific data.
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000429
430
Christian Heimesfe337bf2008-03-23 21:54:12 +0000431.. attribute:: kevent.udata
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000432
Georg Brandlc5605df2009-08-13 08:26:44 +0000433 User defined value.