/* Issue all requests simultaneously; there's no blocking. */
$delay=15;
$id=0;
while ($delay > 0) {
$s=stream_socket_client("phaseit.net:80", $errno,
$errstr, $timeout,
STREAM_CLIENT_ASYNC_CONNECT|STREAM_CLIENT_CONNECT);
if ($s) {
$sockets[$id++]=$s;
$http_message="GET /demonstration/delay?delay=" .
$delay . " HTTP/1.0\r\nHost: phaseit.net\r\n\r\n";
fwrite($s, $http_message);
} else {
echo "Stream " . $id . " failed to open correctly.";
}
$delay -= 3;
}
while (count($sockets)) {
$read=$sockets;
stream_select($read, $w=null, $e=null, $timeout);
if (count($read)) {
/* stream_select generally shuffles $read, so we need to
compute from which socket(s) we're reading. */
foreach ($read as $r) {
$id=array_search($r, $sockets);
$data=fread($r, $convenient_read_block);
/* A socket is readable either because it has
data to read, OR because it's at EOF. */
if (strlen($data) == 0) {
echo "Stream " . $id . " closes at " . date('h:i:s') . ".\n";
fclose($r);
unset($sockets[$id]);
} else {
$result[$id] .= $data;
}
}
} else {
/* A time-out means that *all* streams have failed
to receive a response. */
echo "Time-out!\n";
break;
}
}
?>
如果运行此清单,您将看到如下所示的输出。
清单 2. 从清单 1 中的程序获得的典型输出
以下为引用的内容:
Program starts at 02:38:50.
Stream 4 closes at 02:38:53.
Stream 3 closes at 02:38:56.
Stream 2 closes at 02:38:59.
Stream 1 closes at 02:39:02.
Stream 0 closes at 02:39:05.
了解这其中的工作原理至关重要。在较高层次上,第一个程序将发出几个 HTTP 请求并接收 Web 服务器发送给它的页面。虽然生产应用程序将很可能寻找若干个 Web 服务器的地址 —— 可能是 google.com、yahoo.com、ask.com 等 —— 但是此示例将把它的所有请求发送到位于 Phaseit.net 的企业服务器上,只为降低复杂度。
注意,这没有对主机 CPU 造成任何负担。经常会遇到这样一些连网程序,以 CPU 使用率急速上升至 100% 的方式在 while 中使用 fread。那种情况不会出现在这里,因为 stream_select 拥有支持立即响应所需的属性(只要有任何读取信息),但是它将在各读取操作间隙的等待时间内产生可忽略的 CPU 负载。
以下为引用的内容:
<?php
echo "Program starts at ". date('h:i:s') . ".\n";
$timeout=10;
$streams=array();
$handles=array();
/* First launch a program with a delay of three seconds, then
one which returns after only one second. */
$delay=3;
for ($id=0; $id <= 1; $id++) {
$error_log="/tmp/error" . $id . ".txt"
$descriptorspec=array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("file", $error_log, "w")
);
$cmd='sleep ' . $delay . '; echo "Finished with delay of ' .
$delay . '".';
$handles[$id]=proc_open($cmd, $descriptorspec, $pipes);
$streams[$id]=$pipes[1];
$all_pipes[$id]=$pipes;
$delay -= 2;
}
while (count($streams)) {
$read=$streams;
stream_select($read, $w=null, $e=null, $timeout);
foreach ($read as $r) {
$id=array_search($r, $strea**ms);
echo stream_get_contents($all_pipes[$id][1]);
if (feof($r)) {
fclose($all_pipes[$id][0]);
fclose($all_pipes[$id][1]);
$return_value=proc_close($handles[$id]);
unset($streams[$id]);
}
}
}
?>
此程序将生成如下输出:
以下为引用的内容:
Program starts at 10:28:41.
Finished with delay of 1.
Finished with delay of 3.