1

我使用 proc_open 从 WP 插件调用一个慢速 Python 脚本作为子进程(回声混音代码),以及在这个问题上找到的一些代码的变体。

(python)脚本需要大约一分钟来处理(一堆音频),我希望找到一种方法来将输出显示到浏览器,因为它是从 python 脚本打印的。就目前而言,我的整个函数的输出在 proc_open 和 stream_select 进程结束之前不会显示。这甚至包括在函数开头的 echo 语句。

<?php
echo "Why wait before printing me out?";

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

$application_system = "python ";
$application_name .= "glitcher/glitchmix.py";
$application = $application_system.$application_name.$separator;

$argv1 = 'a variable';
$argv2 = 'another variable';
$separator = " ";

$pipes = array();

$proc = proc_open ( $application.$separator.$argv1.$separator.$argv2, $description , $pipes, glitch_player_DIR);

// set all streams to non blockin mode
stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);

// get PID via get_status call
$status = proc_get_status($proc);
// status check here if($status === FALSE)

$pid = $status['pid'];
// now, poll for childs termination
while(true) {
    // detect if the child has terminated - the php way
    $status = proc_get_status($proc);
    //  retval checks : ($status === FALSE) and ($status['running'] === FALSE)

    // read from childs stdout and stderr
    // avoid *forever* blocking through using a time out (1sec)
    foreach(array(1, 2) as $desc) {
        // check stdout for data
        $read = array($pipes[$desc]);
        $write = NULL;
        $except = NULL;
        $tv = 1;
        $n = stream_select($read, $write, $except, $tv);
        if($n > 0) {
            do {
                $data = fgets($pipes[$desc], 8092);
                echo $data . "\n<br/>";
            } while (strlen($data) > 0);
        }
    }
}
?>

是否可以使用对 stream_select 的多次调用来回显子进程输出?

显然,我是套接字编程的新手,并期待从 SO 社区获得更多见解。

4

1 回答 1

0

这是有趣和简单的原因。请参阅Python 的-u选项。我在同一个问题上浪费了 2 天时间。当我用 bash 替换 python 并测试 bash 脚本的一些输出时,它会立即收到它。

于 2015-07-14T13:23:32.100 回答