首先看参数 descriptorspec 的说明 number and the value represents how PHP will pass that descriptor to the child process. 0 is stdin, 1 is stdout, while 2 is stderr. 一个索引的数组的键表示的描述符的数量和价值是如何PHP将通过描述符给子进程。0是标准输入,1是标准输出,而2是stderr。 你的是 2 => array("pipe", "r") 没有给错误出路 再看手册中的范例$descriptorspec = array( 0 => array("pipe", "r"), // stdin is a pipe that the child will read from 1 => array("pipe", "w"), // stdout is a pipe that the child will write to 2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to ); 将出现的错误写入了文件所以你至少要在调试时记录下可能出现的错误
这里改绝对路径试试。
php /home/xxx/web/1.php 这种。
1.php<?php
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "r")
);
$process = proc_open("php 2.php", $descriptorspec, $pipes, null, null);
echo ("Start process:\n");
if (is_resource($process))
{
fwrite($pipes[0], "start\n"); // send start
echo ("\n\nStart ....".stream_get_contents($pipes[1])); //get answer
fwrite($pipes[0], "get\n"); // send get
echo ("Get: ".fgets($pipes[1],4096)); //get answer
fwrite($pipes[0], "stop\n"); //send stop
echo ("\n\nStop ....".fgets($pipes[1],4096)); //get answer fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
$return_value = proc_close($process); //stop test_gen.php
echo ("Returned:".$return_value."\n");
}
?>2.php<?php
$keys=0;
function play_stop()
{
global $keys;
$stdin_stat_arr=fstat(STDIN);
if($stdin_stat_arr[size]!=0)
{
$val_in=fread(STDIN,1024);
switch($val_in)
{
case "start\n": echo "Started\n";
return false;
break;
case "stop\n": echo "Stopped\n";
$keys=0;
return false;
break;
case "pause\n": echo "Paused\n";
return false;
break;
case "get\n": echo ($keys."\n");
return true;
break;
default: echo("Передан не верный параметр: ".$val_in."\n");
return true;
exit();
}
}else{return true;}
}
while(true)
{
while(play_stop()){usleep(1000);}
while(play_stop()){$keys++;usleep(10);}
}
?>
number and the value represents how PHP will pass that descriptor to the child process. 0 is stdin, 1 is stdout, while 2 is stderr.
一个索引的数组的键表示的描述符的数量和价值是如何PHP将通过描述符给子进程。0是标准输入,1是标准输出,而2是stderr。
你的是 2 => array("pipe", "r") 没有给错误出路
再看手册中的范例$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
);
将出现的错误写入了文件所以你至少要在调试时记录下可能出现的错误
函数用错了$stdin_stat_arr = file_get_contents('php://input');
或许可以
https://bugs.php.net/bug.php?id=23424
我看过一个博客,但是地址记不得了,他也遇到了这个问题,但是他是用了ffflush(),然后就OK了,我这里试了一下,没效果,实在不行考虑换个别的方法来实现好了