简体   繁体   中英

Sub process output not being returned 'real-time'

I'm just testing out the Symfony Process Component and it doesn't seem to do what it says on the tin.

Docs state

When executing a long running command (like rsync-ing files to a remote server), you can give feedback to the end user in real-time by passing an anonymous function to the run() method:

I have 1 file with the following

$process = new Process('php terminal.php');
$process->setTimeout(null);
$process->run( function ( $type, $buffer ) {
    if (Process::ERR === $type) {
        echo 'ERR > '.$buffer;
    } else {
        echo 'OUT > '.$buffer;
    }
});

and another with the following

$x = 10;
while( $x ) {
    echo "{$x}\n";
    sleep(1);
    $x--;
}

While running this via terminal after a 10 second wait the output is

OUT > 10
OUT > 9
OUT > 8
OUT > 7
OUT > 6
OUT > 5
OUT > 4
OUT > 3
OUT > 2
OUT > 1

which shows there are 10 iterations of closure above but there is no output until the process is finished.

Am I missing something here?

Regards, Luke

You need to use :

flush();
ob_flush();

On your first code :

$process = new Process('php terminal.php');
$process->setTimeout(null);
$process->run( function ( $type, $buffer ) {
    if (Process::ERR === $type) {
        echo 'ERR > '.$buffer;
    } else {
        echo 'OUT > '.$buffer;
    }
    flush();
    ob_flush();
});

On the 2nd :

$x = 10;
while( $x ) {
    echo "{$x}\n";
    flush();
    ob_flush();
    sleep(1);
    $x--;
}

More information at php.net : ob_flush and flush

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM