proc_open を使用して WP プラグインから子プロセス (ehonest リミックス コード) として遅い python スクリプトを呼び出しています。
(python)スクリプトの処理には約1分かかります(一連のオーディオ)。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 コミュニティからのさらなる洞察を楽しみにしています。