PHP 7.2.0 Release Candidate 2 Released

stream_select

(PHP 4 >= 4.3.0, PHP 5, PHP 7)

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

Descrierea

int stream_select ( array &$read , array &$write , array &$except , int $tv_sec [, int $tv_usec = 0 ] )

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

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:

<?php
/* Prepare the read array */
$read   = array($stream1$stream2);
$write  NULL;
$except NULL;
if (
false === ($num_changed_streams stream_select($read$write$except0))) {
    
/* Error handling */
} elseif ($num_changed_streams 0) {
    
/* At least on one of the streams something interesting happened */
}
?>

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$e0);
?>

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:

<?php
$e 
NULL;
if (
false === stream_select($r$w$e0)) {
    echo 
"stream_select() failed\n";
}
?>

Notă:

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.

A se vedea și

add a note add a note

User Contributed Notes 15 notes

up
5
maartenwolzak at gmail dot com
10 years ago
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

// The function to calculate the timeout
function calctimeout($maxtime, $starttime)
{
   return (
$maxtime - ((microtime(true) - $starttime) * 1000000))/1000000;
}

?>
up
2
bluej100@gmail
10 years ago
@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.
up
2
Ben
10 years ago
You can key on file descriptors just fine by casting them to an int or a string, which returns what you would expect.
up
1
rob at associatedtechs dot com
3 years ago
If you try to use stream_select() with fread(), you may get bit by a combination of bugs (https://bugs.php.net/bug.php?id=52602 and https://bugs.php.net/bug.php?id=51056). As of PHP 5.5.10, fread() and stream_select() don't reliably play well togetherw to divide tIf you need woe" 800amplr1select&am2pe timeout value is the maximum time that will elaps.mve tIfe.g. TLS),h fread(), yo> func thvon">luer the socket() don woe" 800amplrtors'o an can hy byrw to dspan>eme that will elaps.mve tIt bitm<r clstr; tream
up
1
5092 "class="user">rob at associatedtechs duncyincom
15-03-14 ago<3strong>
If you try to use streamPednge the c,t fixpect.de clajust f "() d" r iv ctaan cladecrempan cl elap_<?php

// The function to calculate thlapse; umenexecumeoutwilm ip reduc="default">?>

ass="keyword">- ((null;
}

// The function to calculate th e causeass="ed="default">?>

aass="keywword">- ((null(true) - // The function to calculatOrigim caULL
?>

ayword">= Array(?>

aorig_/>
;
}

// The function to calculate the timeout
function calctimeout($maxtime, $starttime) * $maxtime - ((microtime(true) - $starttime) * 1000000))/$maxtime ($r, $w = null, $x - ((null, $x ))/$x ($maxtime, $starttime) * null))/// The function to calculatcausethr to .

// The function>$r// The function>$starttime
) *
// The function to calculat$/>
alked="defapan>// The function>// The function to calculatpe timeout va ode>NULwill ehpcode">cof ywo"defapan>// The functionan>
ayword">= Array(
?>

aorig_/>
;
}

?>
up
<="842 sclass="user">rob at associatedtechs dakscom
30-11-10 07:5strong>
If you try to use streampe timeout valuecaukscdecand&vely ta hawra of tarme POSIX the giv2n woe" 800amplBuo bet ne:s="par the giv2nnstantaneouslyifier noine by casting thearray orlem ion "v ctale subp redu sleep"fpurpoo consota aitIt bit"Whappen:ape timeout valu: Nlase timt they dw fortead usitI****"the slld Nmenkeyw,e is lemWONT sleep,ely, indicating the . So
up
h162 sclass="user">rob at associatedtechs daidancom
16-12-07 07:53strong>
Note that you should change the cahpcoclassclaw the maxpt>g n
<?php
$r = Array($at youfail>= Array($starttime, $stream2at youfail>= Array($starttime);
$stream2at youfail>= Array($starttime) - ?>

aypartord">= Array(?>

a = Array(?>

aE= Array(?>

a/>
;
}

?>

a derd">= Array(?>

n class="keyword">($r,>$w ,>$x , 1337))/$r$maxtime $starttime))/$r= Array($starttime, // The functionn>= Array(?>

="cols="ar(
$r, $starttime
;
}

// The function to calculat$just // The function>?pan>
up
5
rob at associatedtechs dmark2xvstrong>
33 year0 ago52strong>
If you try to use stream_selec'ss
&l> - Set blowpar_buffck(la>
- Set bloycolo_buffck(la>
em hwm clapp
up
5
92503 .class="user">rob at associatedtechs djwA handy syntas="genanchor" href="#115400"> ¶
19year27 10:5/strong>
If you try to use streamp.net/bug.phpid=51056). As of PHP 5.4268="_blank">https://bugs.php.net/bug.phpid=51056). As of PHP 5.4268=tle=">
up
5
92>32 .class="user">rob at associatedtechs duncar /rible dneindi>
32 te" title="2007-02-01 05:35">19year04 07:4/strong>
Note that you should change the cacode claon">< 9ilgis aene byould e ifss="e an d usears of a tream,sxpir-ockqacode.pan claH goodpe timeout va * ind*urning oeams ="pattiion ipe,e is clfor snnodm hapows 9e. Inshde.
up
5
h814 .class="user">rob at associatedtechs dMaxda727tusss="genanchor" href="#72796"> ¶
10 y9-29 10:28strong>
Note that you should chaS ta hape timeout va wra of .. Rstream resofirst NOT a t at they,e is sr sty referenc2tnot is just(Sothe cass a Slssclaident fewwe cacoceer ows )="default"><?php
$r function calctimeout($r, $r, $starttime($starttime))/// The functionn>= Array(?>

a arserd">= Array(
?>

color: #007700">;

$r= Array($starttime= Array($starttime))/?>

ayword">= null($r(// The functionclass="key="keyword">(?>

ayord">(?>

a/>;

// The functionn>;
}

?>

ayord">(?>

aclaord">, $maxtime ($r($starttime, $x , $x ($r= Array($starttime))/$maxtime , ?>

ajusord">))/?>

a/"keyword">(?>

word">))/// The functionn>= Array(?>

aclaord">,
$r(?>

word">))/?>

aasT a gord">= Array(= Array(at youfaifoo"tord">= Array($starttime,>$x = Array($starttime(// The function to calculatCclateme tULL<) T a .ass="kord">function $maxtime = null($r, $x , $starttime))/// The function to calculaSr styneywnot is he modifmhe canv cs ofs neywows ,e is $justnot"paramhifoo",rns ibar", depene claould e pass a .="defapan>// The function>$x , $x , $x ,>$maxtime , $starttime))/$x ;>?pan>
up
0a href="#115400" class="name"> rob at associatedtechs dasuncar /dsgm/a>
36 year4 09:28strong>
Note that you should chaMak=
up
- rob at associatedtechs ddoyouitwb at r /A haisainers>
35ye6-17 10:
If you try to use stream_select fread(), you may get bitatm go wpartrega cstriNOT a t115ip/spate=upan claUfstrin class="keyw get bitm< pan claOng for t, <-/> me ye
erstring,< goy ugs.pI magte=upan claIffix usmyistrea is nowncormanua tilartmiong/dsevrrorI ru retuot is issuentation foupan claWcolsee streaumentreamr is ene claupet bitapplicn defa51056w/div>
add a note add a note

e-noteoidaspan cl'laeletp mau'iv> v> e-not
  • e-nooooooooooooooooooooooooooooo v> e-nooooooooooooooooooov> e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" et-/>
    func​ction" r class="toooooooooooooooooooo-/iv> e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" rc='/ibu
    e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" rc='/ibu
    e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" rc='/ibu
    e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" rc='/ibu
    e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" rc='/ihpcodxtphclateel="rd> e-nooooooooooooooooooooo-set-blocking.php" rc='/ihpcodxtpgr nspan> e-nooooooooooooooooooooo-set-blocking.php" rc='/ihpcodxtpgr nopul="rd>cln class​hpcodxts​gunc​oporlass="toooooooooooooooooooo-/iv> e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" rc='/ihpcodxtpgr ny ref>ul="rd>cln class​hpcodxts​gunc​y ref>orlass="toooooooooooooooooooo-/iv> e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" rc='/ihpcodxtpsr nspan> e-nooooooooooooooooooooo-set-blocking.php" rc='/ihpcodxtpsr nop e-nooooooooooooooooooooo-set-blocking.php" rc='/ihpcodxtpsr ny ref>ul="rd>cln class​hpcodxts​sunc​y ref>orlass="toooooooooooooooooooo-/iv> e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" rc='/ihppy-to-src='/notesd> e-nooooooooooooooooooooo-set-blocking.php" rc='/idec3d" rel="rd> e-nooooooooooooooooooooo-set-blocking.php" rc='/ifilterna ofndel="rd> e-nooooooooooooooooooooo-set-blocking.php" rc='/ifilternpreofndel="rd> e-nooooooooooooooooooooo-set-blocking.php" rc='/ifilternregisrenel="rd> e-nooooooooooooooooooooo-set-blocking.php" rc='/ifilternremoveel="rd> e-nooooooooooooooooooooo-set-blocking.php" rc='/igr nhpcode">el="rd>cln class​gunc​hpcode">orlass="toooooooooooooooooooo-/iv> e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" rc='/igr nfilter>el="rd>cln class​gunc​filter>orlass="toooooooooooooooooooo-/iv> e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" rc='/igr nlinoel="rd> e-nooooooooooooooooooooo-set-blocking.php" rc='/igr nmeta-ows l="rd>ws cln class​gunc​metac​>ws r:lass="toooooooooooooooooooo-/iv> e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" rc='/igr ntransv ct>el="rd>cln class​gunc​transv ct>r:lass="toooooooooooooooooooo-/iv> e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" rc='/igr nwra of >el="rd>cln class​gunc​wra of >r:lass="toooooooooooooooooooo-/iv> e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" rc='/iis-localel="rd> e-nooooooooooooooooooooo-set-blocking.php" rc='/inotificn def-callbackel="rd> e-nooooooooooooooooooooo-set-blocking.php" rc='/iregisrennwra of el="rd> e-nooooooooooooooooooooo-set-blocking.php" rc='/iresolve-includeny thel="rd> e-nooooooooooooooooooooo-set-blocking.php" rc='/images/notesd> e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" rc='/isr nction" rel="rd> e-nooooooooooooooooooooo-set-blocking.php" rc='/isr nchunk-sizoel="rd> e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" rc='/isr nwpar-buffckel="rd> e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" rc='/isr ns="keywel="rd> e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" rc='/isr no <-buffckel="rd> e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" rc='/is>
    func​accandorlass="toooooooooooooooooooo-/iv> e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" rc='/is>
    func​clicalorlass="toooooooooooooooooooo-/iv> e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" rc='/is>
    func​etgle c​cryptoorlass="toooooooooooooooooooo-/iv> e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" rc='/is>
    class​=> func​gunc​userorlass="toooooooooooooooooooo-/iv> e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" rc='/is>
    class​=> func​paikorlass="toooooooooooooooooooo-/iv> e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" rc='/is>
    el="rd> func​recvon">"> class​=> func​recvon">orlass="toooooooooooooooooooo-/iv> e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" rc='/is>
    class​=> func​sendtoorlass="toooooooooooooooooooo-/iv> e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" rc='/is>
    class​=> func​servckorlass="toooooooooooooooooooo-/iv> e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" rc='/is>
    class​=> func​shut e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" rc='/isupv ct>-dd> l="rd>c​lockcln class​=upv ct>c​lockorlass="toooooooooooooooooooo-/iv> e-nooooooooooooooooo class="fun"iv> e-nooooooooooooooooooooo-set-blocking.php" rc='/iwra of nregisrenel="rd> e-nooooooooooooooooooooo-set-blocking.php" rc='/iwra of nrestorrel="rd> e-nooooooooooooooooooooo-set-blocking.php" rc='/iwra of nunregisrenel="rd> e-noov>