Descrierea

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

Parametri

read

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).

write

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

except

The streams listed in the except array will be
watched for high priority exceptional ("out-of-band") data arriving.

Notă:

When stream_select() returns, the arrays
read, write and
except are modified to indicate which stream
resource(s) 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.

tv_sec

The tv_sec and tv_usec
together form the timeout parameter,
tv_sec specifies the number of seconds while
tv_usec the number of microseconds.
The timeout is an upper bound on the amount of time
that stream_select() will wait before it returns.
If tv_sec and tv_usec are
both set to 0, stream_select() will
not wait for data - instead it will return immediately, indicating the
current status of the streams.

If tv_sec is NULLstream_select()
can block indefinitely, returning only when an event on one of the
watched streams occurs (or if a signal interrupts the system call).

Avertizare

Using a timeout value of 0 allows you to
instantaneously poll the status of the streams, however, it is NOT a
good idea to use a 0 timeout value in a loop as it
will cause your script to consume too much CPU time.

It is much better to specify a timeout value of a few seconds, although
if you need to be checking and running other code concurrently, using a
timeout value of at least 200000 microseconds will
help reduce the CPU usage of your script.

Remember that the timeout value is the maximum time that will elapse;
stream_select() will return as soon as the
requested streams are ready for use.

tv_usec

See tv_sec description.

Valorile întoarse

On success stream_select() returns the number of
stream resources contained in the modified arrays, which may be zero if
the timeout expires before anything interesting happens. On error FALSE
is returned and a warning raised (this can happen if the system call is
interrupted by an incoming signal).

Exemple

Example #1 stream_select() Example

This example checks to see if data has arrived for reading on either
$stream1 or $stream2.
Since the timeout value is 0 it will return
immediately:

Note

Notă:

Due to 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:

<?php$e = NULL;stream_select($r, $w, $e, 0);?>

Notă:

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 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.

Notă:

Some streams (like zlib) cannot be selected by this
function.

Notă:

Windows compatibility: stream_select() used on a
pipe returned from proc_open() may cause data loss
under Windows 98.

Use of stream_select() on
file descriptors returned by proc_open() will fail
and return FALSE under Windows.

Please note that, on return, the key of "read" will be zero based, serially numbered according to the streams for which there is read data ready only. In other words, if you want to know which of the original streams placed in "read" is ready, there is no immediate way of knowing that.

If you want to know which of the original stream is which, you can either use "==", or possibly set a reverse map array, in which the stream is the key, and the key to the original "read" array is the data.

If you use stream_select() with a blocking stream, you are doing it wrong!

Just because this function returns something in one or more of the arrays does NOT mean that a future read or write operation will not block.

The above sentence is the most important sentence you will ever read regarding stream manipulation. Using stream_select() with blocking streams is a very common amateur mistake and causes major headaches when tracking down usage of this and similar select() system functions. PHP (and really the underlying OS) should verify that the supplied stream set is not blocking and throw an error/exception if any socket is set to block so that people are forced to fix their code. The documentation for stream_select() is, at best, misleading.

If you want a non-blocking stream, then set the stream to not block. Otherwise, live with the blocking stream. That is, after all, the whole point of blocking - to block indefinitely until the operation completes. select() is built for non-blocking streams ONLY. Any other use will result in very hard to track down bugs.

I got the above lecture many years ago after encountering the very bugs I mention. I fixed my code and now correct similar mistakes when I run into the issue elsewhere. Writing code for non-blocking streams is simpler than trying to write hacks for blocking streams with select() functions and ending up with application bugs.

Note that contrary to what the previous poster said, one is not able to use a stream resource as a key for an array. Rather, if you want to know which socket you are dealing with, consider using code similar to this:

Note that reading from a regular file which is on end-of-file will *not* block. You'll get a non-blocking, zero-byte read. However, stream_select *will* block if the input is a pipe, and there is no more data to be had.

$streams = array("foo" => $stream_one, "bar" => $stream_two); // Create an array of two (already existant) streams.if($new = select($streams, $key, 60)){ //Sets $new to the resource that next gets new data, and $key to either "foo", or "bar", depending which one it is.echo $key.":".stream_get_line($new, 2048)."\n";}?>

stream_select() looks deceptively like a simple wrapper around POSIX select(2).

But beware: while select(2) allows you to pass no file descriptors and use it as a "portable subsecond sleep", PHP will complain with "Warning: stream_select(): No stream arrays were passed in ****" if all arrays are empty or null, and it WONT sleep, it will return immediately. So... if the number of file descriptors you have isn't static, you have to deal with the special case yourself.