📄 function.stream-select.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Runs the equivalent of the select() system call on the given arrays of streams with a timeout specified by tv_sec and tv_usec</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.stream-resolve-include-path.html">stream_resolve_include_path</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.stream-set-blocking.html">stream_set_blocking</a></div> <div class="up"><a href="ref.stream.html">Stream Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.stream-select" class="refentry"> <div class="refnamediv"> <h1 class="refname">stream_select</h1> <p class="verinfo">(PHP 4 >= 4.3.0, PHP 5)</p><p class="refpurpose"><span class="refname">stream_select</span> — <span class="dc-title">Runs the equivalent of the select() system call on the given arrays of streams with a timeout specified by tv_sec and tv_usec </span></p> </div> <div class="refsect1 unknown"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><b><b>stream_select</b></b></span> ( <span class="methodparam"><span class="type">array</span> <tt class="parameter reference">&$read</tt></span> , <span class="methodparam"><span class="type">array</span> <tt class="parameter reference">&$write</tt></span> , <span class="methodparam"><span class="type">array</span> <tt class="parameter reference">&$except</tt></span> , <span class="methodparam"><span class="type">int</span> <tt class="parameter">$tv_sec</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$tv_usec</tt></span> ] )</div> <p class="simpara"> The <b>stream_select()</b> function accepts arrays of streams and waits for them to change status. Its operation is equivalent to that of the <a href="function.socket-select.html" class="function">socket_select()</a> function except in that it acts on streams. </p> <p class="simpara"> The streams listed in the <i><tt class="parameter">read</tt></i> 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 <a href="function.fread.html" class="function">fread()</a> will return a zero length string). </p> <p class="simpara"> The streams listed in the <i><tt class="parameter">write</tt></i> array will be watched to see if a write will not block. </p> <p class="simpara"> The streams listed in the <i><tt class="parameter">except</tt></i> array will be watched for high priority exceptional ("out-of-band") data arriving. </p> <blockquote><p><b class="note">Note</b>: When <b>stream_select()</b> returns, the arrays <i><tt class="parameter">read</tt></i>, <i><tt class="parameter">write</tt></i> and <i><tt class="parameter">except</tt></i> are modified to indicate which stream resource(s) actually changed status. <br /> </p></blockquote> <p class="simpara"> The <i><tt class="parameter">tv_sec</tt></i> and <i><tt class="parameter">tv_usec</tt></i> together form the <em class="emphasis">timeout</em> parameter, <i><tt class="parameter">tv_sec</tt></i> specifies the number of seconds while <i><tt class="parameter">tv_usec</tt></i> the number of microseconds. The <em class="emphasis">timeout</em> is an upper bound on the amount of time that <b>stream_select()</b> will wait before it returns. If <i><tt class="parameter">tv_sec</tt></i> and <i><tt class="parameter">tv_usec</tt></i> are both set to <i>0</i>, <b>stream_select()</b> will not wait for data - instead it will return immediately, indicating the current status of the streams. If <i><tt class="parameter">tv_sec</tt></i> is <b><tt>NULL</tt></b> <b>stream_select()</b> can block indefinitely, returning only when an event on one of the watched streams occurs (or if a signal interrupts the system call). </p> <p class="simpara"> On success <b>stream_select()</b> 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 <b><tt>FALSE</tt></b> is returned and a warning raised (this can happen if the system call is interrupted by an incoming signal). </p> <div class="warning"><b class="warning">Warning</b> <p class="para"> Using a timeout value of <i>0</i> allows you to instantaneously poll the status of the streams, however, it is NOT a good idea to use a <i>0</i> timeout value in a loop as it will cause your script to consume too much CPU time. </p> <p class="para"> 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 <i>200000</i> microseconds will help reduce the CPU usage of your script. </p> <p class="para"> Remember that the timeout value is the maximum time that will elapse; <b>stream_select()</b> will return as soon as the requested streams are ready for use. </p> </div> <p class="simpara"> You do not need to pass every array to <b>stream_select()</b>. You can leave it out and use an empty array or <b><tt>NULL</tt></b> instead. Also do not forget that those arrays are passed <em class="emphasis">by reference</em> and will be modified after <b>stream_select()</b> returns. </p> <p class="para"> This example checks to see if data has arrived for reading on either <i><tt class="parameter">$stream1</tt></i> or <i><tt class="parameter">$stream2</tt></i>. Since the timeout value is <i>0</i> it will return immediately: <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/* Prepare the read array */<br /></span><span style="color: #0000BB">$read </span><span style="color: #007700">= array(</span><span style="color: #0000BB">$stream1</span><span style="color: #007700">, </span><span style="color: #0000BB">$stream2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$write </span><span style="color: #007700">= </span><span style="color: #0000BB">NULL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$except </span><span style="color: #007700">= </span><span style="color: #0000BB">NULL</span><span style="color: #007700">;<br />if (</span><span style="color: #0000BB">false </span><span style="color: #007700">=== (</span><span style="color: #0000BB">$num_changed_streams </span><span style="color: #007700">= </span><span style="color: #0000BB">stream_select</span><span style="color: #007700">(</span><span style="color: #0000BB">$read</span><span style="color: #007700">, </span><span style="color: #0000BB">$write</span><span style="color: #007700">, </span><span style="color: #0000BB">$except</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">))) {<br /> </span><span style="color: #FF8000">/* Error handling */<br /></span><span style="color: #007700">} elseif (</span><span style="color: #0000BB">$num_changed_streams </span><span style="color: #007700">> </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">/* At least on one of the streams something interesting happened */<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </p> <blockquote><p><b class="note">Note</b>: Due to a limitation in the current Zend Engine it is not possible to pass a constant modifier like <b><tt>NULL</tt></b> 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: <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$e </span><span style="color: #007700">= </span><span style="color: #0000BB">NULL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">stream_select</span><span style="color: #007700">(</span><span style="color: #0000BB">$r</span><span style="color: #007700">, </span><span style="color: #0000BB">$w</span><span style="color: #007700">, </span><span style="color: #0000BB">$e</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <br /> </p></blockquote> <blockquote><p><b class="note">Note</b>: Be sure to use the <i>===</i> operator when checking for an error. Since the <b>stream_select()</b> may return 0 the comparison with <i>==</i> would evaluate to <b><tt>TRUE</tt></b>: <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$e </span><span style="color: #007700">= </span><span style="color: #0000BB">NULL</span><span style="color: #007700">;<br />if (</span><span style="color: #0000BB">false </span><span style="color: #007700">=== </span><span style="color: #0000BB">stream_select</span><span style="color: #007700">(</span><span style="color: #0000BB">$r</span><span style="color: #007700">, </span><span style="color: #0000BB">$w</span><span style="color: #007700">, </span><span style="color: #0000BB">$e</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">"stream_select() failed\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <br /> </p></blockquote> <blockquote><p><b class="note">Note</b>: 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. <br /> </p></blockquote> <blockquote><p><b class="note">Note</b>: Windows compatibility: <b>stream_select()</b> used on a pipe returned from <a href="function.proc-open.html" class="function">proc_open()</a> may cause data loss under Windows 98. <br /> Use of <b>stream_select()</b> on file descriptors returned by <a href="function.proc-open.html" class="function">proc_open()</a> will fail and return <b><tt>FALSE</tt></b> under Windows. <br /> </p></blockquote> <p class="para"> See also <a href="function.stream-set-blocking.html" class="function">stream_set_blocking()</a>. </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.stream-resolve-include-path.html">stream_resolve_include_path</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.stream-set-blocking.html">stream_set_blocking</a></div> <div class="up"><a href="ref.stream.html">Stream Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -