|
|
stream_select (PHP 4 >= 4.3.0, PHP 5) 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 Descriptionint stream_select ( array &read, array &write, array &except, int tv_sec [, int tv_usec] )
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.
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 high priority exceptional ("out-of-band") data arriving.
Замечание:
When stream_select() returns, the arrays
read, write and
except are modified to indicate which stream
resource(s) actually changed status.
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 NULL stream_select()
can block indefinitely, returning only when an event on one of the
watched streams occurs (or if a signal interrupts the system call).
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).
| Внимание |
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.
|
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.
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:
|
<?php
$read = array($stream1, $stream2);
if (false === ($num_changed_streams = stream_select($read, $write = NULL, $except = NULL, 0))) {
} elseif ($num_changed_streams > 0) {
}
?>
|
Замечание:
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
stream_select($r, $w, $e = NULL, 0);
?>
|
Замечание:
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:
|
<?php
if (false === stream_select($r, $w, $e = NULL, 0)) {
echo "stream_select() failed\n";
}
?>
|
Замечание:
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.
Замечание:
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.
See also
stream_set_blocking().
Maxdamantus
29-Sep-2007 10:28
Simple stream_select wrapper.. Returns the first stream in the array, and sets parameter 2 to the key (So that it is easy to identify what received data):
<?php
function select($array, &$vkey, $timeout=0){
$select = array();
$null = NULL;
foreach($array as $key => $sock){
$x = count($select);
$select[$x] = $sock;
$keys[$x] = $key;
}
if(stream_select($select, $null, $null, $timeout)){
foreach($keys as $key){
if($array[$key] == $select[0]){
$vkey = $key;
return($select[0]);
}
}
}
}
$streams = array("foo" => $stream_one, "bar" => $stream_two); if($new = select($streams, $key, 60)){ echo $key.":".stream_get_line($new, 2048)."\n";
}
?>
bluej100@gmail
21-Aug-2007 04:31
@mbaynton at gmail dot com
A handy syntactic trick:
<?php
$r = Array($stream1, $stream2);
stream_select($r, $w = null, $x = null, 1337);
?>
I've seen it recommended elsewhere in the documentation for clarifying magic arguments so maintainers don't have to go check the function itself, but it also solves your problem here.
mbaynton at gmail dot com
17-Aug-2007 07:35
The documentation states that:
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.
This appears to be incorrect, at least in PHP 5.1.6, because PHP sees these parameters are passed by reference and thus insists on a bona fide variable for the reference to refer to:
Fatal error: Only variables can be passed by reference
is what you get if you pass NULL or anything else literally.
Ben
01-Feb-2007 05:35
You can key on file descriptors just fine by casting them to an int or a string, which returns what you would expect.
maartenwolzak at gmail dot com
25-Jan-2007 01:36
Note that you should change the calctimeout function below to divide the outcome by 1.000.000 otherwise you'll be waiting for two years instead of one minute for the socket to timeout...
<?php
function calctimeout($maxtime, $starttime)
{
return ($maxtime - ((microtime(true) - $starttime) * 1000000))/1000000;
}
?>
aidan at aidans dot org
07-Dec-2006 07:53
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:
<?php
$sockets = array("sock_1" => $sock1, "sock_2" => $sock2, "sock_3" => $sock_3);
$read = $write = $error = $sockets;
$num = stream_select($read, $write, $error, 10);
if ($n > 0) {
foreach ($read as $r) {
$key = array_search($r, $sockets);
}
}
?>
Hope that helps someone out there!
phpdoc at shemesh dot biz
14-Mar-2005 01:33
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.
aidan at php dot net
22-Jun-2004 11:30
If you want to set an absolute maximum execution time for stream_select in a loop, it's important to decrement the max_time value passed to stream_select.
<?php
$maxtime = 200000;
$starttime = microtime(true);
$r = $orig_sockets;
function calctimeout($maxtime, $starttime)
{
return $maxtime - ((microtime(true) - $starttime) * 1000000);
}
while (stream_select($r, $w = null, $e = null, 0, calctimeout($maxtime, $starttime)) !== 0)
{
foreach ($r as $socket) {
}
$r = $orig_sockets;
}
?>
|