[ Stream I/O | Reference Manual | Alphabetic Index ]

# stream_select(++StreamList, +Timeout, -ReadyStreams)

Returns streams from StreamList which are ready for I/O, blocking at most Timeout seconds.

StreamList
  A list of atoms (stream aliases) or stream handles.
Timeout
  A number or an atom.
ReadyStreams
  A term unifiable with a list of atoms or stream handles.

## Description

stream_select/3 is modelled after (and partially implemented using) the select() Unix system call. StreamList is a list of streams where input or output is expected to occur. If I/O is available on some streams from the StreamList, ReadyStreams is unified with a list of those. The same symbolic stream names are used in both lists.

If Timeout is a number, it must be non-negative and less than 100000000. stream_select/3 then waits for at most Timeout seconds for I/O on these streams and if none is available, it unifies ReadyStreams with nil. If Timeout is zero, stream_select/3 does not wait but it immediately returns the list of streams where I/O is available. If Timeout is the atom 'block', stream_select/3 waits until I/O is possible on one of the streams in StreamList.

The streams in StreamList can be sockets, queues, string streams or the null stream. On Unix systems, pipes, files and ttys are also allowed. On Windows, ttys (the console) is also allowed, but only when timeout is 0.

Notes:

- Sockets are only considered ready when input (rather than output) is possible.
- The null stream is never ready.
- Unlike the select() system call, stream_select/3 does not test any exceptional pending conditions on sockets.

### Modes and Determinism

- stream_select(++, +, -) is det

### Exceptions

(4) instantiation fault
  StreamList is not instantiated or it contains uninstantiated variables.
(4) instantiation fault
  Timeout is not instantiated.
(5) type error
  StreamList is not a list of stream handles and atoms.
(5) type error
  Timeout instantiated, but not to a number or an atom.
(5) type error
  ReadyStreams is instantiated, but not to a list or nil
(6) out of range
  Timeout is a negative number or an atom different from block.
(141) unimplemented functionality
  Not implemented for this stream class on this system.
(170) system interface error
  The system call was interrupted by a signal.
(192) illegal stream mode
  A stream in StreamList is not an input stream or a pipe, or it is a string stream or null.
(193) illegal stream specification
  A stream in StreamList is not open or does not exist.

## Examples

         ?- socket(internet, datagram, s), bind(s, _/40000),
            socket(internet, datagram, r), bind(r, _/40001),
            stream_select([s, r], block, Streams).

         <blocks until data arrives>

         ?- open(queue(""), update, q).
         yes.

         ?- stream_select([q],0,L).
         L = []
         yes.

         ?- write(q,hello).
         yes.

         ?- stream_select([q],0,L).
         L = [q]
         yes.

## See Also

open / 3, open / 4, socket / 3, get_stream_info / 3