i have command run using exec() returns value within large data file, has run millions of times. in order avoid having file opened each time in loop, want move proc_open
-based solution, file pointer created once efficiency, can't work out how this.
here exec
-based version, works slow, presumably because has open file in each iteration:
foreach ($locations $location) { $command = "gdallocationinfo -valonly -wgs84 datafile.img {$location['lon']} {$location['lat']}"; echo exec ($command); }
my attempt @ proc_open
-based code follows:
$descriptorspec = array ( 0 => array ('pipe', 'r'), // stdin - pipe child read 1 => array ('pipe', 'w'), // stdout - pipe child write // 2 => array ('file', '/tmp/errors.txt', 'a'), // stderr - file write ); $command = "gdallocationinfo -valonly -wgs84 datafile.img"; $fp = proc_open ($command, $descriptorspec, $pipes); foreach ($locations $location) { fwrite ($pipes[0], "{$location['lon']} {$location['lat']}\n"); fclose ($pipes[0]); echo stream_get_contents ($pipes[1]); fclose ($pipes[1]); } proc_close ($fp);
this correctly gets first value, generates error each subsequent iteration:
3.3904595375061 warning: fwrite(): 6 not valid stream resource in file.php on line 11 warning: fclose(): 6 not valid stream resource in file.php on line 12 warning: stream_get_contents(): 7 not valid stream resource in file.php on line 13 warning: fclose(): 7 not valid stream resource in file.php on line 14 warning: fwrite(): 6 not valid stream resource in file.php on line 11 ...
- you're not "opening file" you're executing process. if process not designed handle multiple requests within scope of single execution you're not going able around
proc_open()
or else. in following block you're closing both input , output pipes of process, , yet you're surprised when you're no longer able read or write?
foreach ($locations $location) { fwrite ($pipes[0], "{$location['lon']} {$location['lat']}\n"); fclose ($pipes[0]); // here echo stream_get_contents ($pipes[1]); fclose ($pipes[1]); // , here }
try not doing that.
Comments
Post a Comment