Php 在Perl脚本仍在运行时在浏览器中显示控制台输出

Php 在Perl脚本仍在运行时在浏览器中显示控制台输出,php,perl,Php,Perl,这是我正在测试的一个非常简单的程序 #!/usr/bin/perl print "This is Test Script\n"; sleep(5); print "This is Test Script\n"; sleep(5); print "This is Test Script\n"; sleep(5); print "This is Test Script\n"; sleep(5); print "This is Test Script\n"; sleep(5); print

这是我正在测试的一个非常简单的程序

#!/usr/bin/perl

print "This is Test Script\n";
sleep(5);

print "This is Test Script\n";
sleep(5);

print "This is Test Script\n";
sleep(5);

print "This is Test Script\n";
sleep(5);

print "This is Test Script\n";
sleep(5);

print "Script Testing Done\n";
现在,PHP应该每隔10秒或5秒或者每当PHP在控制台上看到输出时输出脚本输出(它将进入控制台)

我有数百个perl脚本,我无法更改这些脚本并将输出定向到一个文件,php/ajax可以在该文件中获取内容并将其输出到浏览器

谢谢

您可能想要和

前者允许您随意读取/写入进程。后者刷新输出缓冲区

(编辑,添加示例代码)

下面是一个调用上面的Perl示例的PHP脚本示例(假设其名为
test.pl
)。注意,由于Perl的输出缓冲机制,您需要告诉您的Perl脚本使STDOUT隐式刷新(或者在Perl语言中“使其变热”)。您可以通过在Perl脚本的顶部添加
$|=1
来实现这一点

<?php

$descriptor = array(
   0 => array("pipe", "r"), // stdin
   1 => array("pipe", "w"), // stdout
   2 => array("pipe", "w"), // stderr
);

$proc = proc_open('./test.pl', $descriptor, $pipes);
if ($proc) {
    fclose($pipes[0]); // no input
    stream_set_blocking($pipes[1], 0); // turn off blocking
    while (!feof($pipes[1])) {
        echo "PHP> (heartbeat)\n";
        $fromPerl = fread($pipes[1], 1024); // read up to 1k
        if ($fromPerl) {
            echo "Perl> {$fromPerl}";
        }
        sleep(2); // do other work instead
    }
    proc_close($proc);
}

我可能在这里找错了方向,但你不可能是吗?

嗯,回到过去,我会使用CGI和未解析的标题(NPH…谷歌it)以及
$|=1
“当你想让你的管道滚烫时”


您必须对PHP端执行类似的操作。

等等,什么?您正在使用PHP调用Perl脚本?这里有一个非常类似的问题(答案很好):是的,我正在从PHP调用Perl脚本。嗨,Scoates,您能提供一些代码吗?我如何使用这两个函数?我添加了一个示例脚本及其输出。非常感谢Scoates…这个示例太棒了。。我真的很感激。如果它回答了你的问题,你应该接受它(-:如果没有,哪一部分还不清楚?+1@scoates:但是有没有任何理由返回成功/失败标志并停止perl进程之类的,提前谢谢
$ time php proc.php 
PHP> (heartbeat)
PHP> (heartbeat)
Perl> This is Test Script
PHP> (heartbeat)
PHP> (heartbeat)
Perl> This is Test Script
PHP> (heartbeat)
PHP> (heartbeat)
PHP> (heartbeat)
Perl> This is Test Script
PHP> (heartbeat)
PHP> (heartbeat)
Perl> This is Test Script
PHP> (heartbeat)
PHP> (heartbeat)
PHP> (heartbeat)
Perl> This is Test Script
PHP> (heartbeat)
PHP> (heartbeat)
Perl> Script Testing Done
PHP> (heartbeat)

real    0m30.031s
user    0m0.020s
sys 0m0.018s