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