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