http://cn.php.net/proc_open的以下这一段就是原来我也在玩的一个想法!很有意思!
启动一堆php子进程,这些子进程全在监听标准输入,主进程有任务时再把数据扔给子进程!这样应该比每次都生成一堆子进程来得节省资源!
jaroslaw at pobox dot sk
28-Mar-2008 06:15
Some functions stops working proc_open() to me.
This i made to work for me to communicate between two php scripts:
<?php
$abs_path = '/var/www/domain/filename.php';
$spec = array(array("pipe", "r"), array("pipe", "w"), array("pipe", "w"));
$process = proc_open('php '.$abs_path, $spec, $pipes, null, $_ENV);
if (is_resource($process)) {
sleep(1);
fwrite($pipes[0], 'echo $test;');
fflush($pipes[0]);
usleep(1000);
echo fread($pipes[1],1024).'<hr>';
fclose($pipes[0]);fclose($pipes[1]);fclose($pipes[2]);
$return_value = proc_close($process);
}
?>
filename.php then contains this:
<?php
$test = 'test data generated here<br>';
while(true) {
if($fh = fopen('php://stdin','rb')) {
$val_in = fread($fh,1024);
fclose($fh);
}
if($val_in)
eval($val_in);
usleep(1000);
if($tmp_counter++ > 100)
break;
}
?>