Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In PHP I'm Unable to pipe from one process to another

Tags:

php

pipe

timer

In timer.php I have this:

$handle = fopen( 'php://stdout', 'wa' ) ;    
$unusedEvTimerObject = new EvTimer(0, 1, function ($watchercallback)    use ($handle) { //create & call timer
    echo "=>".(Ev::iteration() % 60)."<=";
    fwrite( $handle, "Hello World! \n");
} );
  Ev::run();
fclose( $handle );

And in child.php I have this:

$descriptorspec = array(
    0 => array("pipe", "r"),  
    1 => array("pipe", "w"),          
    2 => array("file", "/tmp/error-output.txt", "a")
);

$process = proc_open('php ', $descriptorspec, $pipes);

if (is_resource($process)) {
    fwrite($pipes[0], "<? include('app/timer.php'); ?>");
    fclose($pipes[0]);

    $output = "";
    while (!feof($pipes[1])) {
        $output .= fgets($pipes[1]);
    };
    fclose($pipes[1]);

    $return_value = proc_close($process);

    echo "command returned $return_value\n";
}

If I invoke timer.php direct with

$php app/timer.php

the output I get is "=>1<=Hello World! =>2<=Hello World!"

but if I invoke with $php app/child.php I get no output whereas I'd expect stdout from timer.php to redirect to child.php and be printed by it.

I'm flailing a bit & I'm guessing child.php is not getting any input but I can't see why. HALP!

like image 587
DavidH Avatar asked Jul 19 '15 22:07

DavidH


1 Answers

The $output isn't printed in the sample code.

while (!feof($pipes[1])) {
    echo fgets($pipes[1]);
};

Calling $> php child.php then prints the timer output

like image 93
Jason Hendry Avatar answered Oct 17 '22 06:10

Jason Hendry