非常に大きなデータ ファイル内から値を返す exec() を使用してコマンドを実行しましたが、何百万回も実行する必要があります。ファイルがループで毎回開かれるのを避けるために、proc_open
効率のためにファイルポインタが一度作成されるベースのソリューションに移行したいのですが、これを行う方法がわかりません。
これは、おそらく各反復でファイルを開く必要があるため、機能しますexec
が遅いバージョンです。
foreach ($locations as $location) {
$command = "gdallocationinfo -valonly -wgs84 datafile.img {$location['lon']} {$location['lat']}";
echo exec ($command);
}
ベースのコードでの私の試みproc_open
は次のとおりです。
$descriptorspec = array (
0 => array ('pipe', 'r'), // stdin - pipe that the child will read from
1 => array ('pipe', 'w'), // stdout - pipe that the child will write to
// 2 => array ('file', '/tmp/errors.txt', 'a'), // stderr - file to write to
);
$command = "gdallocationinfo -valonly -wgs84 datafile.img";
$fp = proc_open ($command, $descriptorspec, $pipes);
foreach ($locations as $location) {
fwrite ($pipes[0], "{$location['lon']} {$location['lat']}\n");
fclose ($pipes[0]);
echo stream_get_contents ($pipes[1]);
fclose ($pipes[1]);
}
proc_close ($fp);
これは最初の値を正しく取得しますが、その後の反復ごとにエラーを生成します。
3.3904595375061
Warning: fwrite(): 6 is not a valid stream resource in file.php on line 11
Warning: fclose(): 6 is not a valid stream resource in file.php on line 12
Warning: stream_get_contents(): 7 is not a valid stream resource in file.php on line 13
Warning: fclose(): 7 is not a valid stream resource in file.php on line 14
Warning: fwrite(): 6 is not a valid stream resource in file.php on line 11
...