阿里技术学习博客

分享学习经验

IT博客 联系 聚合 管理
  42 Posts :: 1 Stories :: 36 Comments :: 0 Trackbacks

 

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)) {
   
# wait till something happens on other side
   
sleep(1);
   
# send command
   
fwrite($pipes[0], 'echo $test;');
   
fflush($pipes[0]);
   
# wait till something happens on other side
   
usleep(1000);
   
# read pipe for result
   
echo fread($pipes[1],1024).'<hr>';
   
# close pipes
   
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) {
   
# read incoming command
   
if($fh = fopen('php://stdin','rb')) {
       
$val_in = fread($fh,1024);
       
fclose($fh);
    }
   
# execute incoming command
   
if($val_in)
        eval(
$val_in);
   
usleep(1000);
   
# prevent neverending cycle
   
if($tmp_counter++ > 100)
        break;
}
?>

 

posted on 2008-12-03 16:22 阿里爸爸 阅读(1199) 评论(1)  编辑 收藏 引用 所属分类: 技术

Feedback

# re: 使用php监听标准输入的方式,实现实时调用的方式 2008-12-13 16:40 dell笔记本
谢谢,正需要这个  回复  更多评论
  

只有注册用户登录后才能发表评论。