blob: b79f5c2d406815508af72328d8ee2cac12c6cbf6 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{select} ---
Fred Drakebbac4321999-02-20 00:14:17 +00002 Waiting for I/O completion}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drakebbac4321999-02-20 00:14:17 +00004\declaremodule{builtin}{select}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{Wait for I/O completion on multiple streams.}
6
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00007
Andrew M. Kuchling99a37012000-08-25 01:21:28 +00008This module provides access to the \cfunction{select()}
9and \cfunction{poll()} functions
Guido van Rossum9814a941998-09-28 14:28:38 +000010available in most operating systems. Note that on Windows, it only
11works for sockets; on other operating systems, it also works for other
12file types (in particular, on \UNIX{}, it works on pipes). It cannot
Fred Drake38e5d272000-04-03 20:13:55 +000013be used on regular files to determine whether a file has grown since
Guido van Rossum9814a941998-09-28 14:28:38 +000014it was last read.
15
16The module defines the following:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000017
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000018\begin{excdesc}{error}
19The exception raised when an error occurs. The accompanying value is
Fred Drakeb4016371998-04-02 18:44:38 +000020a pair containing the numeric error code from \cdata{errno} and the
21corresponding string, as would be printed by the \C{} function
22\cfunction{perror()}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000023\end{excdesc}
24
Andrew M. Kuchling99a37012000-08-25 01:21:28 +000025\begin{funcdesc}{poll}{}
26(Not supported by all operating systems.) Returns a polling object,
27which supports registering and unregistering file descriptors, and
28then polling them for I/O events;
29see section~\ref{poll-objects} below for the methods supported by
30polling objects.
31\end{funcdesc}
32
Fred Drakecce10901998-03-17 06:33:25 +000033\begin{funcdesc}{select}{iwtd, owtd, ewtd\optional{, timeout}}
Fred Drakeb4016371998-04-02 18:44:38 +000034This is a straightforward interface to the \UNIX{} \cfunction{select()}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000035system call. The first three arguments are lists of `waitable
Fred Drake5255c792000-12-11 15:50:07 +000036objects': either integers representing file descriptors or
Fred Drakeb4016371998-04-02 18:44:38 +000037objects with a parameterless method named \method{fileno()} returning
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000038such an integer. The three lists of waitable objects are for input,
39output and `exceptional conditions', respectively. Empty lists are
Fred Drake5255c792000-12-11 15:50:07 +000040allowed, but acceptance of three empty lists is platform-dependent.
41(It is known to work on \UNIX{} but not on Windows.) The optional
42\var{timeout} argument specifies a time-out as a floating point number
43in seconds. When the \var{timeout} argument is omitted the function
44blocks until at least one file descriptor is ready. A time-out value
45of zero specifies a poll and never blocks.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000046
47The return value is a triple of lists of objects that are ready:
48subsets of the first three arguments. When the time-out is reached
49without a file descriptor becoming ready, three empty lists are
50returned.
51
52Amongst the acceptable object types in the lists are Python file
Fred Drakeb4016371998-04-02 18:44:38 +000053objects (e.g. \code{sys.stdin}, or objects returned by
54\function{open()} or \function{os.popen()}), socket objects
55returned by \function{socket.socket()},%
56\withsubitem{(in module socket)}{\ttindex{socket()}}
Fred Drake5255c792000-12-11 15:50:07 +000057\withsubitem{(in module os)}{\ttindex{popen()}}.
58You may also define a \dfn{wrapper} class yourself, as long as it has
59an appropriate \method{fileno()} method (that really returns a file
60descriptor, not just a random integer).
61\strong{Note:}\index{WinSock} File objects on Windows are not
62acceptable, but sockets are. On Windows, the underlying
63\cfunction{select()} function is provided by the WinSock library, and
64does not handle file desciptors that don't originate from WinSock.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000065\end{funcdesc}
Andrew M. Kuchling99a37012000-08-25 01:21:28 +000066
67\subsection{Polling Objects
68 \label{poll-objects}}
69
70The \cfunction{poll()} system call, supported on most Unix systems,
71provides better scalability for network servers that service many,
72many clients at the same time.
73\cfunction{poll()} scales better because the system call only
74requires listing the file descriptors of interest, while \cfunction{select()}
75builds a bitmap, turns on bits for the fds of interest, and then
76afterward the whole bitmap has to be linearly scanned again.
77\cfunction{select()} is O(highest file descriptor), while
78\cfunction{poll()} is O(number of file descriptors).
79
80\begin{methoddesc}{register}{fd\optional{, eventmask}}
81Register a file descriptor with the polling object. Future calls to
82the \method{poll()} method will then check whether the file descriptor
83has any pending I/O events. \var{fd} can be either an integer, or an
84object with a \method{fileno()} method that returns an integer. File
85objects implement
86\method{fileno()}, so they can also be used as the argument.
87
88\var{eventmask} is an optional bitmask describing the type of events you
89want to check for, and can be a combination of the constants
90\constant{POLLIN}, \constant{POLLPRI}, and \constant{POLLOUT},
91described in the table below. If not specified, the default value
92used will check for all 3 types of events.
93
94\begin{tableii}{l|l}{code}{Constant}{Meaning}
95 \lineii{POLLIN}{There is data to read}
96 \lineii{POLLPRI}{There is urgent data to read}
97 \lineii{POLLOUT}{Ready for output: writing will not block}
98 \lineii{POLLERR}{Error condition of some sort}
99 \lineii{POLLHUP}{Hung up}
100 \lineii{POLLNVAL}{Invalid request: descriptor not open}
101\end{tableii}
102
103Registering a file descriptor that's already registered is not an
104error, and has the same effect as registering the descriptor exactly
105once.
106
107\end{methoddesc}
108
109\begin{methoddesc}{unregister}{fd}
110Remove a file descriptor being tracked by a polling object. Just like
111the \method{register()} method, \var{fd} can be an integer or an
112object with a \method{fileno()} method that returns an integer.
113
114Attempting to remove a file descriptor that was never registered
115causes a \exception{KeyError} exception to be raised.
116\end{methoddesc}
117
118\begin{methoddesc}{poll}{\optional{timeout}}
119Polls the set of registered file descriptors, and returns a
120possibly-empty list containing \code{(\var{fd}, \var{event})} 2-tuples
121for the descriptors that have events or errors to report.
122\var{fd} is the file descriptor, and \var{event} is a bitmask
123with bits set for the reported events for that descriptor
124--- \constant{POLLIN} for waiting input,
125\constant{POLLOUT} to indicate that the descriptor can be written to, and
126so forth.
127An empty list indicates that the call timed out and no file
128descriptors had any events to report.
129\end{methoddesc}
130
131