blob: f68a0da0915eb61617985c07f01047b833d31370 [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
10available in most operating systems. Note that on Windows, it only works for
11sockets; on other operating systems, it also works for other file types (in
12particular, on Unix, it works on pipes). It cannot be used on regular files to
13determine whether a file has grown since it was last read.
14
15The module defines the following:
16
17
18.. exception:: error
19
20 The exception raised when an error occurs. The accompanying value is a pair
21 containing the numeric error code from :cdata:`errno` and the corresponding
22 string, as would be printed by the C function :cfunc:`perror`.
23
24
25.. function:: poll()
26
27 (Not supported by all operating systems.) Returns a polling object, which
28 supports registering and unregistering file descriptors, and then polling them
29 for I/O events; see section :ref:`poll-objects` below for the methods supported
30 by polling objects.
31
32
33.. function:: select(iwtd, owtd, ewtd[, timeout])
34
35 This is a straightforward interface to the Unix :cfunc:`select` system call.
36 The first three arguments are sequences of 'waitable objects': either
37 integers representing file descriptors or objects with a parameterless method
38 named :meth:`fileno` returning such an integer. The three sequences of
39 waitable objects are for input, output and 'exceptional conditions',
40 respectively. Empty sequences are allowed, but acceptance of three empty
41 sequences is platform-dependent. (It is known to work on Unix but not on
42 Windows.) The optional *timeout* argument specifies a time-out as a floating
43 point number in seconds. When the *timeout* argument is omitted the function
44 blocks until at least one file descriptor is ready. A time-out value of zero
45 specifies a poll and never blocks.
46
47 The return value is a triple of lists of objects that are ready: subsets of the
48 first three arguments. When the time-out is reached without a file descriptor
49 becoming ready, three empty lists are returned.
50
51 .. index::
52 single: socket() (in module socket)
53 single: popen() (in module os)
54
55 Among the acceptable object types in the sequences are Python file objects (e.g.
56 ``sys.stdin``, or objects returned by :func:`open` or :func:`os.popen`), socket
57 objects returned by :func:`socket.socket`. You may also define a :dfn:`wrapper`
58 class yourself, as long as it has an appropriate :meth:`fileno` method (that
59 really returns a file descriptor, not just a random integer).
60
61 .. %
62
63 .. note::
64
65 .. index:: single: WinSock
66
67 File objects on Windows are not acceptable, but sockets are. On Windows, the
68 underlying :cfunc:`select` function is provided by the WinSock library, and does
69 not handle file descriptors that don't originate from WinSock.
70
71
72.. _poll-objects:
73
74Polling Objects
75---------------
76
77The :cfunc:`poll` system call, supported on most Unix systems, provides better
78scalability for network servers that service many, many clients at the same
79time. :cfunc:`poll` scales better because the system call only requires listing
80the file descriptors of interest, while :cfunc:`select` builds a bitmap, turns
81on bits for the fds of interest, and then afterward the whole bitmap has to be
82linearly scanned again. :cfunc:`select` is O(highest file descriptor), while
83:cfunc:`poll` is O(number of file descriptors).
84
85
86.. method:: poll.register(fd[, eventmask])
87
88 Register a file descriptor with the polling object. Future calls to the
89 :meth:`poll` method will then check whether the file descriptor has any pending
90 I/O events. *fd* can be either an integer, or an object with a :meth:`fileno`
91 method that returns an integer. File objects implement :meth:`fileno`, so they
92 can also be used as the argument.
93
94 *eventmask* is an optional bitmask describing the type of events you want to
95 check for, and can be a combination of the constants :const:`POLLIN`,
96 :const:`POLLPRI`, and :const:`POLLOUT`, described in the table below. If not
97 specified, the default value used will check for all 3 types of events.
98
99 +-------------------+------------------------------------------+
100 | Constant | Meaning |
101 +===================+==========================================+
102 | :const:`POLLIN` | There is data to read |
103 +-------------------+------------------------------------------+
104 | :const:`POLLPRI` | There is urgent data to read |
105 +-------------------+------------------------------------------+
106 | :const:`POLLOUT` | Ready for output: writing will not block |
107 +-------------------+------------------------------------------+
108 | :const:`POLLERR` | Error condition of some sort |
109 +-------------------+------------------------------------------+
110 | :const:`POLLHUP` | Hung up |
111 +-------------------+------------------------------------------+
112 | :const:`POLLNVAL` | Invalid request: descriptor not open |
113 +-------------------+------------------------------------------+
114
115 Registering a file descriptor that's already registered is not an error, and has
116 the same effect as registering the descriptor exactly once.
117
118
119.. method:: poll.unregister(fd)
120
121 Remove a file descriptor being tracked by a polling object. Just like the
122 :meth:`register` method, *fd* can be an integer or an object with a
123 :meth:`fileno` method that returns an integer.
124
125 Attempting to remove a file descriptor that was never registered causes a
126 :exc:`KeyError` exception to be raised.
127
128
129.. method:: poll.poll([timeout])
130
131 Polls the set of registered file descriptors, and returns a possibly-empty list
132 containing ``(fd, event)`` 2-tuples for the descriptors that have events or
133 errors to report. *fd* is the file descriptor, and *event* is a bitmask with
134 bits set for the reported events for that descriptor --- :const:`POLLIN` for
135 waiting input, :const:`POLLOUT` to indicate that the descriptor can be written
136 to, and so forth. An empty list indicates that the call timed out and no file
137 descriptors had any events to report. If *timeout* is given, it specifies the
138 length of time in milliseconds which the system will wait for events before
139 returning. If *timeout* is omitted, negative, or :const:`None`, the call will
140 block until there is an event for this poll object.
141