简体   繁体   English

子流程输出未“实时”返回

[英]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. 我只是在测试Symfony Process Component,它似乎并没有按照锡说的做。

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: 当执行长时间运行的命令(例如将文件同步到远程服务器)时,可以通过将匿名函数传递给run()方法来实时向最终用户提供反馈:

I have 1 file with the following 我有以下文件1

$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 等待10秒钟后通过端子运行此命令时,输出为

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. 这表明上面有10次闭包迭代,但是在过程完成之前没有输出。

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 在php.net上的更多信息: ob_flushflush

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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