主要是pcntl和popen
高负载处理--“三剑客”:
缓存、缓冲、并发

1.使用PCNTL扩展:
process control
主要使用pcntl_fork函数和pcntl_waitpid函数
<?php
function performSomeFunction($n, &$ret){
$pid = getmypid();
echo "this is in {$pid}.{$n}\n";
usleep(500000);
$ret[$pid] = array($pid);
exit(0);
}
$i = 0;
$starttime = microtime(TRUE);
$pid_arr = array();
$ret_arr = array();
$num = intval($argv[1]);
echo $num."\n";
while ($i < $num)
{
$pid = pcntl_fork();
if ($pid == -1)
{
die('could not fork');
}
else
{
if ($pid) // parent
{
$pid_arr[$i] = $pid;
}
else // child
{
performSomeFunction($i+1, $ret_arr);
}
}
$i++;
}

while(count($pid_arr) > 0)
{
$myId = pcntl_waitpid(-1, $status, WNOHANG);
foreach ($pid_arr as $key => $pid)
{
if ($myId == $pid)
unset($pid_arr[$key]);
}
usleep(100);
}

$elapsed = microtime(TRUE) - $starttime;
print "\n==> total elapsed: " . sprintf("%f secs.\n", $elapsed);
?>

注明:pcntl_fork(手册介绍:http://php.net/manual/en/function.pcntl-fork.php)起进程起来并发处理,然后底层做了测试,效果也还不错,但是当通过apache调用的时候却无法运行。然后看到了pcntl_fork手册有下面一句:
It is not possible to use the function 'pcntl_fork' when PHP is used as Apache module. You can only use pcntl_fork in CGI mode or from command-line.

2.使用popen
popen打开进程文件指针
<?php
$num = intval($argv[1]);
$i = 0;
$retArr = array();
$generateNumberArr = array();
while ($i++ < $num) {
$generateNumberArr[$i] = popen("/home/users/gino/apps/php5/bin/php /home/users/gino/test/pcntl/one.php " .$i, 'r');
}

foreach ($generateNumberArr as $generateNumberArr_k =>$generateNumberArr_v) {
$str = '';
while (! feof($generateNumberArr_v)) {
$str .= fgets($generateNumberArr_v, 4096);
}
$retArr[] = $str;
pclose($generateNumberArr_v);

}
var_dump($retArr);
?>