stream_select

(PHP 4 >= 4.3.0)

stream_select -- Runs the equivalent of the select() system call on the given arrays of streams with a timeout specified by tv_sec and tv_usec

Description

int stream_select ( resource &read, resource &write, resource &except, int tv_sec [, int tv_usec])

The stream_select() function accepts arrays of streams and waits for them to change status. Its opperation is equivalent to that of the socket_select() function except in that it acts on streams.

The streams listed in the read array will be watched to see if characters become available for reading (more precisely, to see if a read will not block - in particular, a stream resource is also ready on end-of-file, in which case an fread() will return a zero length string).

The streams listed in the write array will be watched to see if a write will not block.

The streams listed in the except array will be watched for exceptions.

Внимание

On exit, the arrays are modified to indicate which stream resource actually changed status.

You do not need to pass every array to stream_select(). You can leave it out and use an empty array or NULL instead. Also do not forget that those arrays are passed by reference and will be modified after stream_select() returns.

Example:
/* Prepare the read array */
$read = array($stream1, $stream2);

if (false === ($num_changed_streams = stream_select($read, $write = NULL, $except = NULL, 0))) {
    /* Error handling */
else if ($num_changed_streams > 0) {
    /* At least on one of the streams something interesting happened */
}

Замечание: Due a limitation in the current Zend Engine it is not possible to pass a constant modifier like NULL directly as a parameter to a function which expects this parameter to be passed by reference. Instead use a temporary variable or an expression with the leftmost member being a temporary variable:
stream_select($r, $w, $e = NULL, 0);

The tv_sec and tv_usec together form the timeout parameter. The timeout is an upper bound on the amount of time elapsed before stream_select() returns. tv_sec may be zero , causing stream_select() to return immediately. This is useful for polling. If tv_sec is NULL (no timeout), stream_select() can block indefinitely.

On success stream_select() returns the number of stream resorces contained in the modified arrays, which may be zero if the timeout expires before anything interesting happens. On error FALSE is returned.

Замечание: Be sure to use the === operator when checking for an error. Since the stream_select() may return 0 the comparison with == would evaluate to TRUE:
if (false === stream_select($r, $w, $e = NULL, 0)) {
    echo "stream_select() failed\n";
}

Замечание: Be aware that some stream implementations need to be handled very carefully. A few basic rules:

  • You should always try to use stream_select() without timeout. Your program should have nothing to do if there is no data available. Code that depends on timeouts is not usually portable and difficult to debug.

  • If you read/write to a stream returned in the arrays be aware that they do not necessarily read/write the full amount of data you have requested. Be prepared to even only be able to read/write a single byte.

See also stream_set_blocking()