PHP 函数proc_open笔记

 

双管道,多线程执行命令: 使用示例:

test.php:

$descriptorspec=array(
    0=>STDIN,
    1=>STDOUT,
    2=>STDERR
);

$process=proc_open('mysql -u root -p',$descriptorspec,$pipes)

这样 运行php test.php 看见的效果将和终端直接运行mysql命令一模一样。

$descriptorspec说明, 它表述了执行命令的执行文件获得从哪儿获得STDIN以及STDOUT输出到哪儿, STDERR输出到哪儿。

他一般有三种形式, 资源,管道,文件

管道表示:

$descriptorspec=array(
    0=>array('pipe','r'),
    1=>array('pipe','w'),
    2=>array('pipe','w'),
);

管道操作:

if(is_resource($process)){
    fwrite($pipes[0],'content');

    while($ret=fgets[$pipes[1]){
       echo ''.$ret;
    }
    while($ret=fgets[$pipes[2]){
    echo ''.$ret;
}
}
注意事项:

1,读取管道时,有时候不能用stream_get_contents获得内容,必须用fgets, 这时候往往是因为命令执行文件有while死循环,不能结束进程,stream_get_contents是程序结束进程后才能

读取到。 而fgets 每次只读取一行, 要求命令执行文件输出内容需要带换行。 并且有时候需要用while语句循环来读取, 因为fgets每次只读一行 这样保证所有的都读取完了。

2,再读取管道时,往往会堵塞。但是不影响下面程序的执行。 如:

$ret=fgets($pipes[1]);//这里有堵塞。
echo $ret;
fwrite($pipes[0],'content');//但是这里可以执行。

这时候fgets 不能加while循环。


3,stream_set_blocking函数可以设置是否堵塞,但是不知道为什么对管道好像不起作用。
 

4,将读取内容加上空字符串后才进行echo输出的目的, 是将STDERR, STDOUT等特殊类型的字符串转换为标准字符串 ,不然有些程序将读取不了这些字符串,比如用  ob_get_clean() 有可能读取不了。   

 

 
  1. public function __construct($cmd){

  2. $this->pipes = null;

  3. $this->descriptors = array(

  4. 0 => array('pipe', 'r'),

  5. 1 => array('pipe', 'w'),

  6. 2 => array('pipe', 'w')

  7. );

  8. $this->resource = proc_open($cmd, $this->descriptors, $this->pipes);

  9. stream_set_blocking($this->pipes[0], FALSE);

  10. stream_set_blocking($this->pipes[1], FALSE);

  11. }

 

$status = proc_get_status($this->resource);
 
  1. Array

  2. (

  3. [command] => top

  4. [pid] => 7985

  5. [running] => 1

  6. [signaled] =>

  7. [stopped] =>

  8. [exitcode] => -1

  9. [termsig] => 0

  10. [stopsig] => 0

  11. )

 

 
  1. switch($status['exitcode']) {

  2. case '255':

  3. case '-1':

  4. case '1':

  5. return false;

  6. case '0':

  7. // normal end

  8. return false;

  9. default:

  10. // unknown

  11. }

 

 
  1. public function errors(){

  2. $msg = 'Error:';

  3. while (is_resource($this->pipes[self::PIPE_STDOUT]) && !feof($this->pipes[self::PIPE_STDOUT])) {

  4. $msg .= fgets($this->pipes[self::PIPE_STDOUT]);

  5. }

  6. while (is_resource($this->pipes[self::PIPE_STDERR]) && !feof($this->pipes[self::PIPE_STDERR])) {

  7. $msg .= fgets($this->pipes[self::PIPE_STDERR]);

  8. }

  9. fclose($this->pipes[self::PIPE_STDERR]);

  10.      proc_close($this->resource);

  11.   return $msg;

  12. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值